Question presented to candidate: "What makes something a custom hook, and how do you decide when to extract one?"
What a strong answer should cover:
- A custom hook is just a function whose name starts with
useand which calls other hooks. There is no registration and no special API. - The naming convention is load-bearing: it is how the ESLint plugin knows to enforce the Rules of Hooks inside it.
- It shares logic, never state. Every component calling the hook gets its own completely independent state — the single most important thing to say.
- It obeys the Rules of Hooks: call it unconditionally, at the top level, from a component or another hook.
- Why extract one: reusing stateful logic, making a component readable, or making the logic testable on its own.
- What it replaced: HOCs and render props, without the wrapper component, prop collisions, or nesting.
- Composition: hooks call other hooks, so behaviour composes flatly rather than by nesting.
- Return shape: an array when order matters and the caller renames (like
useState), an object when there are several named values. - When not to: a one-off used in a single place, or a wrapper that just renames a built-in hook.
Clarifying questions expected:
- "Is this logic actually reused, or is extraction just for readability?" — both are valid, but they are different arguments.
Code / implementation expected: Yes — a custom hook with state and an effect, used by two components to show state independence.