Rules of react hooks Flashcards

1
Q

Rule 1: Only call hooks at the top level

A

Don’t call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks 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. That’s what allows React to correctly preserve the state of Hooks between multiple useState and useEffect calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Rule 2: Only call hooks from react functions

A

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

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

By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly