React · All levels
React Interview Preparation: Components, State, Hooks and Performance
Key React concepts for frontend interviews — components and props, state, the main hooks, rendering behaviour, keys, forms and performance — explained simply.
Published 4 Dec 20263 min read
React interviews focus on how components render and update. If you can explain when and why a component re-renders, most other questions become easier.
Components and props
A component is a function that returns UI (JSX). Props are inputs passed from a parent; a component must not modify its own props.
function Greeting({ name }) {
return <h1>Hello, {name}</h1>;
}
Q: What is JSX? A syntax extension that looks like HTML and is compiled into JavaScript function calls that create React elements.
State
State is data a component owns that can change over time. Updating state with its setter schedules a re-render.
const [count, setCount] = useState(0);
setCount((c) => c + 1); // functional update — safe when based on previous value
Q: Why not modify state directly? React detects changes by comparing references. Mutating an object or array in place keeps the same reference, so React may not re-render. Always create a new object/array.
When does a component re-render?
- Its state changes.
- Its parent re-renders (by default, children re-render too).
- A context it uses changes.
Re-rendering is not the same as updating the DOM: React compares the new output with the previous one and applies only the differences (reconciliation).
Core hooks
| Hook | Purpose |
|---|---|
useState |
Local state |
useEffect |
Synchronise with things outside React (APIs, subscriptions, timers) |
useRef |
A mutable value that persists without causing re-renders; DOM references |
useMemo |
Cache an expensive calculation between renders |
useCallback |
Cache a function reference between renders |
useContext |
Read a value from context without prop drilling |
useReducer |
Manage complex state transitions with a reducer |
Rules of hooks: call hooks only at the top level of components or custom hooks — never inside conditions or loops — so React can match them up on every render.
useEffect in depth
useEffect(() => {
const id = setInterval(tick, 1000);
return () => clearInterval(id); // clean-up
}, []); // dependency array
- No array → runs after every render.
[]→ runs after the first render (and cleans up on unmount).[a, b]→ runs whenaorbchange.
A common mistake is using an effect to compute values that could be calculated during render. If something can be derived from props or state, compute it directly instead.
Keys in lists
Keys help React identify which items changed, were added or removed. Use a stable unique ID. Using the array index as a key can cause bugs when items are reordered or inserted, because state may attach to the wrong item.
Controlled vs uncontrolled inputs
- Controlled: the input's value comes from React state and changes via
onChange. - Uncontrolled: the DOM keeps the value; you read it with a ref when needed.
Lifting state up and context
When two components need the same data, move the state to their closest common parent and pass it down. For data needed by many components at different depths (theme, logged-in user), use context — but avoid putting rapidly changing values in one large context, as every consumer re-renders.
Performance
React.memo(Component)skips re-rendering when props are shallowly equal.useMemo/useCallbackkeep references stable for memoised children — use them where you have measured a problem.- Split code with
React.lazyandSuspense. - Virtualise very long lists.
Practice checklist
- Build a todo list with add, toggle and delete, using immutable updates.
- Fetch data in an effect with loading and error states, and cancel stale requests.
- Write a custom hook such as
useDebounceoruseLocalStorage. - Explain why a child re-renders and how you would prevent it.
- #react
- #frontend
- #javascript