Overview
React interviews typically probe how well you understand the mental model behind the library, not just its API surface. Interviewers want to see that you can explain why the virtual DOM exists, how reconciliation and keys work together, why hooks have rules, and when to reach for memoization or Context instead of prop drilling. This topic collects the questions that come up most often across junior-to-mid React interviews, paired with answers that go a level deeper than a one-line definition so you can defend your reasoning in a follow-up discussion.
Cricket analogy: A good captain interview isn't about reciting rules, it's explaining why you review a run-out on replay (virtual DOM), why batting order matters for the run chase (reconciliation), and why powerplay fielding restrictions exist (hooks rules).
Frequently Asked Questions
Q: What is the virtual DOM and why does React use it?
The virtual DOM is a lightweight in-memory representation of the real DOM, expressed as plain JavaScript objects. When state changes, React builds a new virtual DOM tree, diffs it against the previous tree (reconciliation), and computes the minimal set of real DOM mutations needed to bring the UI up to date. Direct DOM manipulation is expensive because layout, style recalculation, and repainting are costly browser operations. By batching and minimizing real DOM writes, React trades cheap JavaScript object comparisons for expensive DOM operations, which keeps UI updates fast even as an application grows in complexity.
Cricket analogy: A scorer keeps a lightweight notebook tally (virtual DOM) and only updates the giant electronic scoreboard (real DOM) with the actual changed digits after comparing the new tally to the old one, since repainting the whole board is slow.
Q: How does React's reconciliation algorithm decide what to re-render?
Reconciliation compares the new element tree to the previous one using a heuristic O(n) diffing algorithm rather than the theoretically optimal O(n^3) tree-diff. It relies on two assumptions: elements of different types produce different trees (so React tears down and rebuilds rather than trying to match children), and elements of the same type are compared attribute-by-attribute while their children are diffed recursively. For lists, React uses the key prop to match children across renders instead of relying on index position, which lets it correctly reorder, insert, or remove items without unnecessarily destroying and recreating DOM nodes and component state.
Cricket analogy: Reconciliation is like a scorer quickly comparing this over's ball-by-ball log to last over's using a fast heuristic instead of re-checking every possible combination, and using each fielder's jersey number (key) rather than their position on the field to track who's who when the field changes.
Q: Why are keys important in lists, and what happens if you use array index as a key?
Keys give React a stable identity for each item across renders so it can match old elements to new ones instead of guessing by position. A stable, unique key (like a database id) lets React correctly reuse DOM nodes and preserve component state when items are reordered, inserted, or removed. Using the array index as a key works fine for static lists that never reorder or change length, but it becomes a bug source once items can be added, removed, or reordered: React will match the wrong old element to the wrong new data, causing state to attach to the wrong row, stale form inputs, or incorrect animations.
Cricket analogy: Tracking players by shirt number (stable key) instead of "batting position number" avoids disaster when the order changes mid-innings — using position as identity would wrongly attach one batsman's runs-so-far to whoever now bats at that slot.
Q: What are the Rules of Hooks and why do they exist?
The two rules are: only call hooks at the top level of a function component or custom hook (never inside loops, conditions, or nested functions), and only call hooks from React function components or other custom hooks (never from regular JavaScript functions). These rules exist because React tracks hook state by call order, not by name — internally each component has a linked list of hook calls indexed by position. If a hook is called conditionally, the order can shift between renders, causing React to associate the wrong state or effect with the wrong hook call, leading to subtle and hard-to-debug bugs.
Cricket analogy: A bowler's over sequence must be fixed and unconditional within the over — you can't skip your third ball only sometimes, because the umpire tracks deliveries strictly by position, and skipping one would misattribute a wide to the wrong ball count.
Q: What is the difference between controlled and uncontrolled components?
A controlled component has its form value driven entirely by React state: the input's value prop is set from state and an onChange handler updates that state on every keystroke, making React the single source of truth. An uncontrolled component lets the DOM manage its own internal state, and React reads the current value on demand via a ref, typically using a defaultValue for the initial value instead of value. Controlled components give you validation, conditional disabling, and formatting on every keystroke at the cost of a re-render per input; uncontrolled components are simpler and more performant for cases like a plain file input or a form you only read on submit.
Cricket analogy: A controlled scoreboard has every run entered manually into the official ledger the instant it happens (state-driven), while an uncontrolled scoreboard lets the stadium clock run itself and someone just glances at it (ref) when the over ends.
Q: What does the dependency array in useEffect actually do?
The dependency array tells React when to re-run the effect: after the initial render, React re-runs the effect only if any value in the array has changed (compared with Object.is) since the last render. Omitting the array entirely runs the effect after every render; passing an empty array [] runs it once after the initial mount only. A common mistake is omitting a dependency that the effect actually reads, which causes the effect to close over a stale value — the fix is to include every reactive value the effect uses, or to restructure the effect (for example moving logic into the event handler) so it does not need that dependency at all.
Cricket analogy: A bowling change effect that watches "overs bowled" re-triggers only when that number actually changes; forgetting to watch "pitch condition" too means the captain keeps using stale pitch info from over 5 even at over 30.
Q: What is the difference between useMemo and useCallback?
Both hooks memoize a value between renders based on a dependency array, but they memoize different things: useMemo caches the *result* of an expensive computation and returns that value, while useCallback caches a *function reference* itself, which is really just shorthand for useMemo(() => fn, deps). They're most useful for avoiding expensive recalculations on every render and for keeping stable references so that child components wrapped in React.memo don't re-render just because a new inline function or object was created on the parent. Overusing either adds memory and comparison overhead, so they should be applied where profiling shows a real cost, not by default on every value.
Cricket analogy: useMemo is like pre-calculating a batsman's strike rate once and reusing it until the innings changes, while useCallback is like keeping the same fixed signal (not re-inventing a new hand gesture each ball) so the fielders (memoized children) recognize it instantly without re-reading it — doing this for every trivial signal wastes the coach's time.
Q: What is prop drilling, and how does Context help?
Prop drilling happens when data needs to pass through several intermediate components that don't use the data themselves, just to reach a deeply nested child — each layer has to accept and forward the prop, which adds boilerplate and coupling. The Context API solves this by letting a provider expose a value at the top of a subtree that any descendant can read directly via useContext, skipping the intermediate layers entirely. Context isn't a full replacement for state management libraries, though: every consumer re-renders when the context value changes, so it's best for relatively low-frequency, broadly-shared data like theme, locale, or authenticated user, rather than high-frequency state.
Cricket analogy: Instead of relaying the national anthem schedule through every team official down the line, the stadium PA system (Context provider) broadcasts it once so any player can tune in directly — but broadcasting every single run update this way would make every player react to noise; it's best for slow-changing info like the toss result, not ball-by-ball scores.
Q: What is the difference between state and props?
Props are read-only inputs passed down from a parent component to configure a child; a component must never mutate its own props. State is data owned and managed internally by a component (via useState or useReducer) that can change over time and triggers a re-render when updated. The distinction matters for data flow: props flow one way, top-down, while state changes are local and, if a descendant needs to affect an ancestor, are handled by passing a callback down as a prop ("lifting state up") rather than mutating a prop directly.
Cricket analogy: The batting order (props) is set by the captain and a batsman can't rewrite it, but a batsman's own running tally of runs today (state) is his to update each ball — if he wants the captain to promote him, he signals the captain (lifting state up) rather than editing the order sheet himself.
Q: What are error boundaries and what can't they catch?
Error boundaries are class components that implement static getDerivedStateFromError and/or componentDidCatch to catch JavaScript errors thrown during rendering, in lifecycle methods, and in constructors of their child tree, displaying a fallback UI instead of crashing the whole app. They do not catch errors inside event handlers (those need a regular try/catch), errors in asynchronous code such as setTimeout or promise callbacks, errors during server-side rendering, or errors thrown in the error boundary itself. There is currently no hook-based equivalent, so error boundaries must still be written as class components, though libraries like react-error-boundary wrap this pattern for convenience.
Cricket analogy: A backup umpire (error boundary) steps in only when the on-field umpire makes a decision-review error during play, but can't help if a player gets injured off the field during a press conference (event handler) or during team selection meetings before the match (async/SSR); there's no substitute for the backup umpire role itself.
Quick Reference
- Virtual DOM: in-memory tree diffed against the previous render to compute minimal real DOM updates.
- Reconciliation assumes different element types produce different trees and uses keys to match list items.
- Rules of Hooks: top-level calls only, and only from React functions, because hook order must stay stable.
- Controlled components: value lives in React state; uncontrolled components: value lives in the DOM, read via ref.
- useEffect's dependency array controls when the effect re-runs; missing dependencies cause stale closures.
- useMemo memoizes a computed value; useCallback memoizes a function reference.
- Context avoids prop drilling but re-renders all consumers when its value changes.
- Props are read-only and flow down; state is local and mutable via setters.
- Error boundaries catch render/lifecycle errors in children, not event handler or async errors.
- React batches state updates inside event handlers (and, since React 18, in most async contexts too) to avoid redundant renders.
Key Takeaways
- Be ready to explain the 'why' behind React concepts, not just recite definitions.
- Keys and dependency arrays are two of the most commonly misunderstood — and most commonly tested — topics.
- Know the tradeoffs of Context versus dedicated state management before recommending either.
- Understand what error boundaries cover and, just as importantly, what they don't.
- Practice explaining answers out loud; interviewers weigh clarity of reasoning as much as correctness.
Practice what you learned
1. Why does React use a virtual DOM instead of updating the real DOM directly on every state change?
2. What problem can arise from using array index as a React list key when the list can be reordered?
3. Why must hooks be called at the top level of a component on every render?
4. What is the key functional difference between useMemo and useCallback?
5. Which of the following will an error boundary NOT catch?
Was this page helpful?
You May Also Like
Virtual DOM and Reconciliation
Understand how React's Virtual DOM and reconciliation algorithm efficiently update the real browser DOM.
useMemo and useCallback
Learn how useMemo and useCallback memoize values and functions between renders, and when that memoization is actually worthwhile.
Error Boundaries in React
Catch JavaScript errors in a component tree using class-based error boundaries and display a fallback UI instead of crashing.
Common React Pitfalls
Frequent React mistakes — direct state mutation, bad dependency arrays, index keys, and more — with fixes.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics