What is a custom hook? Write a simple useToggle example.

What it tests: Whether you see hooks as stateful logic extraction. Outline: Define a use-prefixed function calling hooks; write useToggle with useState and callback; explain extraction vs duplication. Red flag: Helpers without hooks or missing use prefix.
What's really being asked
This question evaluates whether you understand custom hooks as React's composition primitive for stateful logic, not just a code organization trick. At senior level, the interviewer cares that you know why the use prefix matters for linting, how custom hooks participate in React's reactive system, and when extracting logic into a hook actually improves maintainability versus adding indirection.
The full answer
Four things in order. First, a crisp definition: a custom hook is a JavaScript function whose name starts with use that can call other hooks. Second, the implementation of useToggle, which should accept an optional initial boolean, call useState to hold the value, and return a tuple where the second item is a stable toggle function created with useCallback so consumers do not re-register effects unnecessarily. Third, the naming and linting contract: the use prefix lets ESLint enforce the Rules of Hooks, checking for unconditional calls and exhaustive dependencies. Fourth, the architectural rationale: extract a custom hook when two or more unrelated components need the same stateful behavior, such as synchronization logic, event subscription, or derived state updates, rather than copying useState and useEffect blocks.
The mistakes people make
Several patterns signal confusion. Returning JSX from a custom hook instead of data and callbacks means you invented a component, not a hook. Writing a plain helper function that does not call useState, useEffect, or other hooks misses the point entirely; it cannot share reactive state. Forgetting the use prefix breaks the Rules of Hooks linter and can lead to subtle bugs if hooks are called conditionally inside what React treats as a normal function. Passing unstable callback references without useCallback is also a red flag because it causes unnecessary effect re-runs in parent components.
What usually comes next
The interviewer may ask how you would test useToggle in isolation, which leads to React Testing Library and rendering a test component that consumes the hook. They might ask how to share state rather than logic, which is a cue to mention Context or external stores. They could also ask about performance, such as whether useMemo is needed around the returned tuple, or how to handle derived toggle states like useToggleArray.
A concrete example
A practical useToggle implementation looks like this. Import useState and useCallback from React. Define the function as useToggle with a parameter initialValue defaulting to false. Inside, create state with const [value, setValue] = useState(initialValue). Create a toggle callback with const toggle = useCallback(() => setValue(v => !v), []). Return [value, toggle] as a tuple. A consuming component calls const [isOpen, toggleOpen] = useToggle() and uses isOpen for conditional rendering and toggleOpen for click handlers. This keeps the boolean state and its inversion logic in one place and makes it trivial to reuse in modals, accordions, or feature flags.
Interview question
Why must a custom hook's name start with use?
- a.So it is allowed to return JSX instead of data
- b.So it can share state directly between unrelated components
- c.So it can be called conditionally inside components
- d.So React's linter can enforce the Rules of HooksCorrect
Why? this is the answer
The use prefix signals to ESLint that the function contains hook calls, enabling the Rules of Hooks linter to verify unconditional calls and correct dependencies. Option B is a common misconception: custom hooks reuse stateful logic, but each call maintains independent state unless combined with Context.
Just read this? Test yourself on what you have been reading.
Read the original → react.dev
- #react
- #custom hooks
- #usestate
- #frontend
- #interview
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.
See open roles