Lesson 16 of 32Question banks
React Interview Questions
Explain rendering, keys, effects, memoisation, and server components the way a strong React candidate does.
React interviews rarely ask you to recite the API. They ask why: why does this component re-render, why does this effect loop forever, why does this list lose its input values. Good answers come from a clear model of how React renders. This lesson gives you that model through the questions that come up most, with live demos you can break.
Rendering and reconciliation
"What is the virtual DOM, and what is reconciliation?" When state changes, React calls your component functions again to get a new tree of elements (plain objects describing UI). It compares the new tree with the previous one (this diffing is reconciliation) and applies only the differences to the real DOM, which is the slow part. "Virtual DOM" is the informal name for that tree of elements.
The diff uses two shortcuts that make it O(n) instead of a full tree comparison:
- If the element type at a position changes (
<div>to<span>, or<Login>to<Profile>), React throws away that whole subtree and its state and builds a new one. - In lists, React matches children by their key, not their position.
"When does a component re-render?" When its own state changes, when its parent re-renders (whether or not props changed), or when a context it reads changes. Re-rendering means calling the function again, not necessarily touching the DOM.
The filter state lives in Dashboard. The user types in the filter box.
Keys
"Why does React need keys, and why not use the index?" Keys tell React which item is which between renders, so it can keep each item's state and DOM node attached to the right data. If you use the array index and the list is reordered, filtered, or prepended, index 0 now refers to a different item, but React keeps the old state at index 0. Use a stable unique ID from the data.
Try it: type something in the first box, then click Add to top. With index keys, your text stays in position 0 and ends up next to the wrong name.
State, props, and where state lives
Model answer: state vs props
Props are inputs passed from a parent; the component reads them and must not change them. State is data the component owns and changes over time with its setter, which triggers a re-render. The same value is often state in one component and a prop in its children. A good rule: if a value can be computed from props or other state, it should not be state at all; compute it during render.
Model answer: lifting state up
When two siblings need the same data, move the state to their closest common parent and pass it down as props, along with callbacks to change it. This keeps one source of truth. If the common parent is far up and many layers just pass props through ("prop drilling"), consider composition (passing components as children) or context.
Model answer: context vs a state library
Context solves passing data deeply without prop drilling. It is ideal for values that change rarely: theme, current user, locale. It is not a performance tool: every consumer re-renders when the context value changes. For frequently changing, widely shared client state, a library like Zustand or Redux Toolkit lets components subscribe to slices. For server data (fetching, caching, refetching), use TanStack Query or framework data fetching instead of putting it in global state at all.
"Controlled vs uncontrolled inputs?" A controlled input gets its value from React state and updates it in onChange, so React is the source of truth (easy validation, formatting, conditional UI). An uncontrolled input keeps its own value in the DOM; you read it with a ref or from the form's FormData on submit. React 19 form actions make uncontrolled forms common again: <form action={fn}> hands you FormData.
Because the input is controlled, the component can transform every keystroke (here, to uppercase) and validate as you type.
Effects and memoisation
"What are common useEffect mistakes?"
- Missing dependencies. The effect reads a stale value from an old render. Include everything it uses; the
react-hooks/exhaustive-depslint rule catches this. - Objects or functions in the dependency array that are recreated every render, making the effect run every render.
- Setting state that is in the dependency array, causing an infinite loop.
- No cleanup for subscriptions, timers, or fetches, causing leaks or race conditions.
- Using an effect when you do not need one: deriving data (compute it during render) or responding to a click (do it in the event handler).
function Profile({ userId }: { userId: string }) {
const [user, setUser] = useState(null);
const options = { id: userId };
useEffect(() => {
fetchUser(options).then(setUser);
}, [options]);
return <p>{user?.name}</p>;
}Model answer: useMemo vs useCallback vs memo
useMemo(() => compute(a, b), [a, b]) caches a computed value between renders. useCallback(fn, [deps]) caches a function (it is useMemo(() => fn, deps)). memo(Component) makes a component skip re-rendering when its props are shallowly equal. They work together: memo on a child is useless if the parent passes a new callback each render, which is what useCallback fixes. Do not memoise everything: measure first. With the React Compiler enabled, most manual memoisation becomes unnecessary.
Match the tool to what it caches or skips.
Server components, hydration, and React 19
Model answer: server vs client components
Server Components (the default in the Next.js App Router) run only on the server. They can be async, read databases and secrets directly, and send no JavaScript for themselves to the browser. Client Components (files starting with "use client") are rendered on the server for the initial HTML and then run in the browser, so they can use state, effects, and event handlers. Push "use client" as far down the tree as possible: keep pages on the server and make only the interactive leaves client components.
"What is hydration?" The server sends HTML so the user sees content immediately. Then React runs the client components in the browser and attaches event handlers to that existing HTML instead of rebuilding it. A hydration mismatch happens when the browser render differs from the server HTML, for example using Date.now(), Math.random(), or window during render. Fix it by rendering the same output on both sides and moving browser-only values into an effect.
Model answer: what is new in React 19
- Actions: async functions passed to
<form action={...}>or used withuseTransition, with pending and error states handled for you. useActionStatefor form state anduseFormStatusfor a submit button's pending state.useOptimisticto show an optimistic update while the request runs.use()to read a promise (with Suspense) or a context, and it can be called conditionally.refas a normal prop, soforwardRefis no longer needed.- Document metadata (
<title>,<meta>) rendered from components, plus stable Server Components and Server Actions.
Debugging performance
"A page feels slow. How do you investigate?" Measure before changing anything:
- Open the React DevTools Profiler, record the slow interaction, and look for components that render often or take long.
- Turn on "Highlight updates when components render" to see unexpected re-renders.
- Check the usual causes: state placed too high, a context value recreated every render, unstable props breaking
memo, rendering thousands of rows. - Fix the cause: move state down, split context, memoise, virtualise long lists (render only visible rows), or use
useTransition/useDeferredValueto keep typing responsive. - For load time, check bundle size, code-split with
lazy, and move work to Server Components.
- reconciliation
- React comparing the new element tree with the previous one to find the minimal DOM changes.
- key
- A stable ID on list items so React matches each item's state and DOM node to the right data.
- controlled input
- An input whose value comes from React state and changes through
onChange. - hydration
- Attaching React's event handlers and state to server-rendered HTML in the browser.
- Server Component
- A component that runs only on the server, can be async, and ships no JavaScript of its own.
- memo
- Wraps a component so it skips re-rendering when its props are shallowly equal.