0
Challenge · 27Hooks & Effects

Custom Hooks

Coreused in ~90% of apps

You've written the same useState + useEffect dance in five components. A custom hook lets you extract that stateful logic once and reuse it everywhere — without copy-paste, without sharing the state.

Core idea. A custom hook is just a function whose name starts with `use` and calls other hooks; it lets you extract and reuse stateful LOGIC between components — each component that calls it still gets its own independent state.
Playground, this isn't a screenshot

Two widgets, one useTogglehook. Flip one — the other doesn't move.

<Notifications />OFFits own state · slot #fresh
<DarkMode />OFFits own state · slot #fresh
// one definition, reused everywhere
function useToggle(initial = false) {
  const [on, setOn] = useState(initial);
  const toggle = () => setOn(o => !o);
  return [on, toggle];
}

A custom hook shares logic, never state. Each call to useToggle() runs its own useState, so every widget keeps a separate, isolated value.

↑ play a little first, then dig in