Sub topic 1
The Rules of Hooks you must keep
Prerequisites
This chapter assumes you can write function components with JSX and pass props. It also assumes you have used useState and useEffect, and that you are comfortable with arrow functions, destructuring and closures.
The two rules
React has two rules for hooks. A hook is a function such as useState that lets a component use React features. Custom hooks follow the same rules.
- Call hooks only at the top level. Do not call them inside conditions, loops, nested functions, or after an early
return. - Call hooks only from React function components or from other hooks. Do not call them from ordinary functions, class components, or event handlers.
Why the rules exist
React does not track hooks by name. It tracks them by position. On the first render, React records "the first hook call was useState, the second was useEffect", and so on. On each later render, it matches your calls to that list in order.
If a call is skipped on one render, every later hook shifts by one slot. Each hook then reads state that belongs to a different hook.
A rule violation and its fix
This component calls a hook after a conditional return. On renders where user is missing, the hook is skipped.
import { useState } from 'react';
function Profile({ user }) {
if (!user) return null;
const [editing, setEditing] = useState(false); // breaks rule 1
return <button onClick={() => setEditing(!editing)}>{user.name}</button>;
}Move the hook above the condition so it runs on every render.
import { useState } from 'react';
function Profile({ user }) {
const [editing, setEditing] = useState(false);
if (!user) return null;
return <button onClick={() => setEditing(!editing)}>{user.name}</button>;
}If you need a hook only sometimes, call it always and branch on its result. You can also move the conditional part into a child component, which then calls the hook unconditionally.
How this applies to custom hooks
A custom hook is a function that calls other hooks. Its name must start with use, such as useToggle. The prefix tells React's tooling, and other developers, that the function follows the rules.
The rules apply in both directions:
- Inside a custom hook, call every built-in hook at the top level.
- Wherever you call a custom hook, treat it like
useState. Call it at the top level of a component or another hook.
import { useState } from 'react';
function useToggle(initial = false) {
const [on, setOn] = useState(initial);
return [on, () => setOn((value) => !value)];
}
function Panel({ enabled }) {
const [open, toggle] = useToggle(); // top level, every render
if (!enabled) return null;
return <button onClick={toggle}>{open ? 'Hide' : 'Show'}</button>;
}A function that does not call any hook should not start with use. The name promises something the function does not do.
Let the linter enforce it
Do not rely on memory. Install eslint-plugin-react-hooks and enable both of its rules.
react-hooks/rules-of-hooksreports conditional calls and calls from the wrong place.react-hooks/exhaustive-depschecks dependency arrays. A later chapter covers it.
Exercise
Exercise
A component contains for (const id of ids) { const [item] = useState(id); }. Which rule does this break, and what is the usual fix?
Show answer
It breaks the rule against calling hooks inside loops. The number of calls changes when ids changes, so React cannot match slots by order. Store one state value that holds all the items, such as an array or object. Or render a child component per id, and let each child call the hook once.