Question presented to candidate: "What is a Higher-Order Component, and would you write one today?"
What a strong answer should cover:
- A HOC is a function that takes a component and returns a new component — a pattern, not an API. React ships nothing called
HOC. - It was the pre-Hooks answer to sharing behaviour across components:
withRouter,connect,withStyles. - The naming convention (
withX) and the rule that a HOC must be pure — it must not mutate the component it receives. - The concrete problems: static methods are not copied,
displayNameis lost so DevTools shows Anonymous, refs do not pass through without forwarding, and prop-name collisions are silent. - "Wrapper hell" — deeply nested HOCs producing an unreadable component tree.
- Typing them in TypeScript is genuinely awkward.
- Custom Hooks replaced them for behaviour sharing: no wrapper component, no collisions, no lost statics, far better typing.
- Where HOCs still legitimately appear: injecting props into a component you do not control, cross-cutting wrappers like error boundaries or analytics, and older library APIs.
Clarifying questions expected:
- "Is this a legacy codebase, or new code?" — the answer changes completely.
- "Are we sharing behaviour, or wrapping rendering?" — Hooks cover the first, HOCs still fit the second.
Code / implementation expected: Yes — a HOC with hoistNonReactStatics-style handling and a displayName, plus the Hook equivalent.