Custom Hooks and Call Order
What you'll learn: How custom hooks plug into React's internal hook registry and why breaking the rules inside them breaks everywhere they're used.
Custom Hooks Are Transparent to React
When you call a custom hook, React doesn't see it as a separate entity—it "sees through" it to the built-in hooks it contains. Every useState, useEffect, or other hook call inside your custom hook gets registered in the same index-based hook chain as if you'd written those hooks directly in your component.
function useToggle(initial) {
const [value, setValue] = useState(initial); // Index 0 in hook chain
const toggle = () => setValue(v => !v); // No hook call here
return [value, toggle];
}
function Component() {
const [open, toggleOpen] = useToggle(false); // useState above becomes index 0
const [count, setCount] = useState(0); // This is index 1
return <div>{open ? count : null}</div>;
}
Why Custom Hook Rules Propagate
Because custom hooks dissolve into their underlying hook calls, any rule violation inside a custom hook becomes a violation in every component that uses it. If useToggle conditionally called useState, every consumer would have unstable hook ordering.
Think of custom hooks like inline functions that get "pasted" into your component at the call site. The hook registry doesn't track "this came from useToggle"—it only tracks "hook call #1, hook call #2..."
This is why the naming convention (use prefix) is enforced: it signals to linters that the function contains hooks and must follow hook rules.
Key Takeaway: Custom hooks are transparent wrappers—their internal hooks become part of the component's hook chain, so rule violations inside them break all consumers.