Interview Q&A / React
React interview questions
Rendering, state, hooks, keys and effects, explained with small examples.
Prefer rapid revision? Practise React short interview answers →
- What is a custom hook, and why would you write one?
A custom hook is a function whose name starts with use and that calls other hooks. It lets you reuse stateful logic across components. Each component that calls it gets its own separate state.
BeginnerQuick answer1 min read - How would you write a simple useFetch custom hook?
Keep data, loading and error in state, fetch inside an effect keyed on the URL, and ignore or abort stale responses in the cleanup. Return the three values so components stay free of fetching details.
IntermediateQuick answer1 min read - How would you write a useDebounce hook for a search box?
Keep a debounced copy of the value in state. In an effect, start a setTimeout that updates the copy after a delay, and clear that timer in the cleanup so only the last change within the delay goes through.
IntermediateQuick answer1 min read