VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 核心概念

  • 高级指引

  • Hook

    • Hooks Overview
    • Using the State Hook
    • Using the Effect Hook
    • Rules of Hooks
      • Only Call Hooks at the Top Level
      • Only Call Hooks from React Functions
      • ESLint Plugin
    • Custom Hooks
  • 案例演示

  • 《React》笔记
  • Hook
xugaoyi
2021-04-06
Contents

Rules of Hooks

# 04. Rules of Hooks

Hooks are JavaScript functions, but you need to follow two rules when using them. We provide a linter plugin (opens new window) to enforce these rules:

# Only Call Hooks at the Top Level

Don't call Hooks inside loops, conditions, or nested functions. Ensure that Hooks are always called at the top level of your React function, before any early returns. By following this rule, you ensure that Hooks are called in the same order each time a component renders.

Tip

  1. How does React know which state corresponds to which useState? The answer is that React relies on the order in which Hooks are called.

  2. If we want to run an effect conditionally, we can put that condition inside our Hook:

  useEffect(function persistForm() {
    // Put the condition inside the effect
    if (name !== '') {
      localStorage.setItem('formData', name);
    }
  });
1
2
3
4
5
6

# Only Call Hooks from React Functions

Don't call Hooks from regular JavaScript functions. Instead, you can:

  • Call Hooks from React function components
  • Call Hooks from custom Hooks

# ESLint Plugin

We released an ESLint plugin called eslint-plugin-react-hooks (opens new window) that enforces these two rules.

npm install eslint-plugin-react-hooks --save-dev
1
// Your ESLint configuration
{
  "plugins": [
    // ...
    "react-hooks"
  ],
  "rules": {
    // ...
    "react-hooks/rules-of-hooks": "error", // Checks rules of Hooks
    "react-hooks/exhaustive-deps": "warn" // Checks effect dependencies
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36
Using the Effect Hook
Custom Hooks

← Using the Effect Hook Custom Hooks→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode