0
Challenge · 28Hooks & Effects
Rules of Hooks
Coreused in ~100% of apps
Put a hook behind an `if` and your app doesn't bend — it breaks: React hands back the wrong state or crashes outright. Two tiny rules keep every hook honest, and they fall straight out of how useState actually remembers things.
Core idea. Only call hooks at the top level of a React function (never in conditions, loops, nested functions, or after an early return), and only from components or custom hooks. React matches each hook to its state by call ORDER, so the order must be identical on every render.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
<Profile /> renders
hook calls this render (in order)
slot 0const [name, setName] = useState("Aria");
if (loggedIn) {
slot 1 const [age, setAge] = useState(30);
}
slot 2const [theme, setTheme] = useState("dark");
slot 0
slot 1
slot 2
React's memory (slots, by order)
0"Aria"
ok130
ok2"dark"
okAll 3 hooks run every render in the same order, so each call lines up with its own slot: name→0, age→1, theme→2. Now flip loggedIn to false and watch the slots realign onto the wrong hooks.
↑ play a little first, then dig in