- React
- Tricky React Output Prediction & Execution Ordering
Tricky React Interview Questions: Output Traps
Tricky React interview questions built on deceptive output prediction snippets are a standard screening tool in frontend technical interviews. They quickly test whether a candidate possesses a precise mental model of React execution or relies on guesswork.
Step through challenging scenarios combining asynchronous state batching, parent-child effect execution ordering, StrictMode double-invocations, and state preservation versus reset behaviors when component keys and tree positions change.
Output prediction questions show a short component and ask what it renders or logs. The four lessons cover the patterns these questions are built on: state as a snapshot with batched updates and stale closures, the order effects and their cleanups run in, including the extra run under StrictMode, React.memo failing because of new object or function props, and state that is kept or reset depending on a component's position in the tree and its key.
Every question can be answered by reasoning about React's rules rather than memorising outputs, and that reasoning is what interviewers are listening for.
Tricky React Interview Questions: 4 lessons
Output Traps: Batching, Snapshots & Updates
React output traps on state batching, snapshots and stale closures: why state updates act as immutable snapshots within one render. Direct updates like setCount(count + 1) evaluate against the captured snapshot value, while functional updaters setCount(c => c + 1) receive the pending state from the queue, preventing stale closure overwrites.
Output Traps: Effect Order, Cleanups & StrictMode
In a parent-child React tree, render phases execute top-down (parent before child), but commit passive effects execute bottom-up (children effects run before parent effects). When dependencies change, previous effect cleanups run before new effects execute.
Output Traps: Memoization & Object Mutations
React.memo and referential identity output traps: memo compares props shallowly with Object.is, so new objects and functions re-render the child. Passing inline object literals, arrays, or anonymous functions creates a new memory reference on every render, bypassing memoization. Preserving referential stability requires useMemo, useCallback, or hoisting constants.
Output Traps: Tree Position & Component Keys
React output traps on tree position and component key identity: state is tied to a component's place in the tree and to its key prop. Changing the key prop forces React to destroy the existing Fiber node, unmount the component, and create a fresh instance with initial state. Keeping identical keys and positions preserves state.
Frequently asked questions
Why does calling setCount(count + 1) three times only add 1?
Each call reads count from the same render, so all three set the same value, and React batches them into one update. Use the updater form, setCount(c => c + 1), so each call builds on the latest queued value.
Why do effects run twice in development?
In StrictMode, React mounts each component, unmounts it and mounts it again in development, to surface effects that do not clean up properly. It happens only in development; in production an effect runs once per mount.
When does React reset a component's state?
When the component is rendered at a different position in the tree, as a different type, or with a different key. Changing the key is the intended way to reset a component's state on purpose.
