Question presented to candidate: "One component throws while rendering and the whole page goes blank. What is the mechanism for handling that?"
What a strong answer should cover:
- An Error Boundary is a component that catches errors thrown while rendering its subtree, and renders a fallback instead of letting the error unmount the whole tree.
- Since React 16, an uncaught render error unmounts the entire root — that is the blank page. Boundaries exist to contain the damage.
- It must be a class component. Two methods:
getDerivedStateFromError(return the fallback state — the render-phase half) andcomponentDidCatch(log it — the commit-phase half, where side effects are allowed). componentDidCatchreceives(error, info)whereinfo.componentStacknames the component that failed — the single most useful thing to send to your logger.- There is no hook version. Libraries wrap a class; the class is still there underneath.
- Placement is the design decision: one boundary at the root only ever gives you a full-page fallback. Boundaries around independently-failing regions — a widget, a route, a sidebar — keep the rest of the page alive.
- A boundary needs a way to recover — a retry button, or a
keychange — or the fallback is permanent. - React 19 added root-level
onUncaughtErrorandonCaughtErroroptions for centralised reporting.
Clarifying questions expected:
- "Which parts of this page should survive if one part fails?" — that determines where boundaries go.
- "Where do we want these errors reported?"
Code / implementation expected: Optional. The two-method class is short enough to write out.