Question presented to candidate: "Is there any reason left to write a class component?"
What a strong answer should cover:
- One real reason: an Error Boundary. Catching a render error requires
getDerivedStateFromErrororcomponentDidCatch, and there is still no hook equivalent. Every popular library wraps a class. - Beyond that, essentially none for new code. Hooks cover state, lifecycle, context and refs, and compose in ways class lifecycle methods cannot.
- Classes are not deprecated and nothing was removed —
ComponentandPureComponentare still exported. Migrating a working class component with no other reason is churn. - The structural argument: class lifecycle methods organise code by timing, so one concern is split across mount, update and unmount. Effects organise by concern, keeping setup and teardown together.
- The reuse argument: the class era had HOCs and render props for sharing stateful logic; a custom hook does it without wrapper components or a nested tree.
- A real behavioural difference to know:
this.setStatemerges the object into state, while auseStatesetter replaces it. That is a genuine migration hazard. - Some modern APIs are hooks-only —
useTransition,useDeferredValue,useSyncExternalStore,use— so a class cannot participate in concurrent features. - Honest exceptions: an existing class codebase, and a team standard.
Clarifying questions expected:
- "New code or an existing codebase?" — the answers are completely different.
- "Is this specifically about error boundaries?" — that is the one genuine case.
Code / implementation expected: Optional. Naming the error boundary exception is the substance.