All interview questions

React Interview Questions

Follow a user interaction from event handler to committed UI. These questions test whether you can explain state and effects before suggesting a fix.

Try answering aloud before opening each explanation.

Try the React practice demo

Foundations

What is the difference between rendering and committing in React?

Rendering calls components to compute UI. Committing applies the resulting changes to the host, such as the browser DOM. A render can be discarded, so it must not have observable side effects.

Why can two setCount(count + 1) calls increment only once?

Both use the same count from the current render snapshot and enqueue the same value. Use two functional updaters when each increment must build on the preceding pending state.

What makes a useful key for a list of editable rows?

Use a stable identity from the data, unique among siblings. A positional key can attach the previous row's local state to a different item when rows move.

What does it mean for an input to be controlled?

React state or props supply its current value and an onChange handler updates the source of truth. An uncontrolled input keeps its current value in the DOM and can start with defaultValue.

When should state be lifted to a common parent?

Lift it when multiple children need to coordinate through one authoritative value. Keep purely local state near its owner instead of making every update flow through a distant component.

Applied

When is an effect unnecessary for a filtered list?

If the list is a pure function of input props and state, compute it during render. An effect that writes derived state adds an update and can briefly show stale data.

How do you avoid leaking subscriptions when an effect dependency changes?

Return cleanup from the effect to unsubscribe the exact listener it registered. React runs cleanup before the next setup for changed dependencies and when removing the component.

When should you use a ref instead of state?

Use a ref for a value that survives renders but does not determine the rendered output, such as a timeout handle. Updating a ref does not schedule a render.

What causes an effect to reconnect on every render?

A dependency may be a fresh object or function each time. Move construction inside the effect or depend on the primitive inputs that determine the connection; do not omit a real dependency.

How do you reset a form when the selected account changes?

If the account defines a distinct form instance, key the form by account ID. React then creates new state instead of momentarily retaining the prior form's edits.

Production scenarios

Why can Strict Mode show an extra setup and cleanup during development?

It tests whether setup can be undone cleanly. Fix missing cleanup or impure work; the development-only check is not a reason to add an ad hoc run-once flag.

Why might memoizing a child fail to reduce its renders?

Fresh object or callback props can defeat shallow comparison, and the child's own state or context can still trigger rendering. Profile the component before adding memoization.

How do you stop obsolete search results replacing current React state?

Abort previous requests where possible and ignore outcomes from outdated effects or request generations. Handle errors for the current request separately from expected cancellation.

What does an error boundary not catch by default?

It handles rendering failures in descendants, not arbitrary event-handler errors or rejected promises in asynchronous callbacks. Handle those failures at their own boundaries.

Why can time-dependent rendering cause a hydration mismatch?

The initial server HTML and initial client render may use different times or locales. Render shared deterministic data first, then update client-specific values after hydration.

Further reading

Official documentation

Related question sets