Hooks deep dive

useEffect dependencies and cleanup, useMemo/useCallback (and when they are noise), useRef, custom hooks. The most probed React topic in interviews.

About 31 min read3-question knowledge check

The mental model

Hooks are how a function component **remembers things between renders** and **synchronizes with the world outside React**. A component function runs from scratch on every render — its local variables are recreated, its body re-executes — so on its own it has no memory. Hooks are the slots where React stores state *for* it across those runs, keyed not by name but by **the order in which you call them**. That last fact is the entire foundation: React does not see const [count, setCount] = useState(0) and remember "count"; it remembers "the first useState call in this component is slot 0", every render, in the same order. Break the order and every subsequent slot points at the wrong memory.

The two hooks people misunderstand, and the correct one-liners:

  • **useState is memory that triggers a re-render when it changes.** Use it for anything the UI must reflect.
  • **useEffect is a synchronization mechanism, not a lifecycle callback.* Its job is to keep something outside* React (a subscription, a DOM node, a network resource, a timer) in sync with React state. It is "after render, if these dependencies changed, run this synchronization; before the next sync or on unmount, clean up." The moment you use it as "run this when the component mounts" or "run this after a button click", you are usually reaching for the wrong tool.

The analogy that sticks: a component is an actor who forgets everything the instant the scene ends. useState is the actor's cue cards, held by a stagehand (React) and handed back in the same order each scene. useEffect is the stage crew who, after the actor finishes a scene, adjusts the set to match what the actor just said — turning on a light (subscribing), and crucially, turning it off before the next scene changes it (cleanup). An effect without cleanup is a light left burning into the next scene; a dependency array that lies about what the effect reads is stage crew acting on last scene's script.

What breaks without this model is a specific, recurring set of bugs: an effect that fetches on every render because a dependency is a new object each time; a stale value captured in a closure because the dependency array excluded it; a subscription that leaks because there was no cleanup; a useEffect doing work that belonged in an event handler or in render itself. Each is useEffect behaving exactly as specified while the developer pictured a lifecycle hook.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision