course content

Course Content

Introduction to React

Rules of Using HooksRules of Using Hooks

There are certain rules we must follow while using Hooks to avoid surprises. For the hooks, there are two simple rules:

  1. Only call hooks at the top level;
  2. Only call hooks from React functions.

Top-level means directly inside the React component, and outside of any other code blocks, for example, loops or conditions. The following code shows valid and invalid cases:

Apart from that, calling React hooks in functions other than React components is also invalid:

Everything was clear?

Section 6. Chapter 4