Skip to content

Lesson 16 of 32Question banks

React Interview Questions

Explain rendering, keys, effects, memoisation, and server components the way a strong React candidate does.

20 min read0/4 questions

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.

Who re-renders when state changes?
AppHeaderFilterBarResultRowResultRow2ResultsDashboardholds filter stateFooterroot

The filter state lives in Dashboard. The user types in the filter box.

1/3
Quick check
A parent re-renders, and its child receives exactly the same props as before. Does the child re-render?
Need a nudge? A hint rules out a wrong answer.

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.

ReactKeysDemo.tsx
import { useState } from "react"; type Person = { id: number; name: string }; export default function App() { const [people, setPeople] = useState<Person[]>([ { id: 1, name: "Ada" }, { id: 2, name: "Sam" }, ]); const [nextId, setNextId] = useState(3); function addToTop() { setPeople([{ id: nextId, name: `New #${nextId}` }, ...people]); setNextId(nextId + 1); } return ( <div style={{ display: "grid", gap: 16, gridTemplateColumns: "1fr 1fr" }}> <button onClick={addToTop} style={{ gridColumn: "1 / 3" }}>Add to top</button> <section> <h4>key = index (buggy)</h4> {people.map((p, i) => ( <label key={i} style={{ display: "block" }}> {p.name} <input placeholder="note" /> </label> ))} </section> <section> <h4>key = id (correct)</h4> {people.map((p) => ( <label key={p.id} style={{ display: "block" }}> {p.name} <input placeholder="note" /> </label> ))} </section> </div> );}

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.

ReactControlled.tsx
import { useState } from "react"; export default function App() { const [name, setName] = useState(""); const tooLong = name.length > 10; return ( <div> <input value={name} onChange={(e) => setName(e.target.value.toUpperCase())} placeholder="Type a name" /> <p>Characters: {name.length} {tooLong && "(too long)"}</p> </div> );}

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-deps lint 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).
Quick check
Why does this component loop forever?
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>;
}
Need a nudge? A hint rules out a wrong answer.
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 pairs0/4 matched

Match the tool to what it caches or skips.

Choose an item, then its match on the other side.

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 with useTransition, with pending and error states handled for you.
  • useActionState for form state and useFormStatus for a submit button's pending state.
  • useOptimistic to show an optimistic update while the request runs.
  • use() to read a promise (with Suspense) or a context, and it can be called conditionally.
  • ref as a normal prop, so forwardRef is no longer needed.
  • Document metadata (<title>, <meta>) rendered from components, plus stable Server Components and Server Actions.
Quick check
Which line in a client component is most likely to cause a hydration mismatch?
Need a nudge? A hint rules out a wrong answer.

Debugging performance

"A page feels slow. How do you investigate?" Measure before changing anything:

  1. Open the React DevTools Profiler, record the slow interaction, and look for components that render often or take long.
  2. Turn on "Highlight updates when components render" to see unexpected re-renders.
  3. Check the usual causes: state placed too high, a context value recreated every render, unstable props breaking memo, rendering thousands of rows.
  4. Fix the cause: move state down, split context, memoise, virtualise long lists (render only visible rows), or use useTransition/useDeferredValue to keep typing responsive.
  5. For load time, check bundle size, code-split with lazy, and move work to Server Components.
Key terms
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.