Full answer
A HOC is a function: const Enhanced = withSomething(Base). It does not change Base. It returns a new component that renders Base and passes extra props, or decides not to render it.
Before hooks, classes could not share stateful logic except through patterns like HOCs and render props. Examples are connect from older Redux, withRouter from older React Router, and permission wrappers such as withAuth.
How to write one properly: forward all props with {...props} so the wrapped component keeps working. Set a display name so React DevTools shows WithAuth(Page). Never create a HOC inside render, because every render would make a new component type and reset state. Copy static methods if needed, and forward refs deliberately, since ref is not a normal prop for older React versions (in React 19 it is a prop for function components).
Problems that hooks solve. Several HOCs wrapped around one component give a deep tree and hard-to-follow props. Two HOCs can inject the same prop name and overwrite each other. The source of a prop is not visible in the component, which makes it harder to read and to type.
A custom hook useAuth() is called inside the component, so the data source is explicit and there is no extra tree layer.
HOCs still make sense for cross-cutting wrappers that decide whether to render at all (feature flags, permission gates) and for code that must wrap class components. The example implements a permission wrapper.
function withPermission(Wrapped, required) {
function WithPermission({ user, ...props }) {
if (!user.permissions.includes(required)) {
return <p>You do not have access.</p>;
}
return <Wrapped {...props} />;
}
WithPermission.displayName = 'WithPermission(' + (Wrapped.displayName || Wrapped.name) + ')';
return WithPermission;
}
function Gradebook({ course }) {
return <h2>Gradebook for {course}</h2>;
}
const TeacherGradebook = withPermission(Gradebook, 'grades:edit');
export default function App() {
const teacher = { name: 'Asha', permissions: ['grades:edit'] };
const student = { name: 'Ravi', permissions: [] };
return (
<div>
<TeacherGradebook user={teacher} course="Maths" />
<TeacherGradebook user={student} course="Maths" />
<p>{TeacherGradebook.displayName}</p>
</div>
);
}withPermission returns a new component that either renders the wrapped one or a message. Rendered result: <div><h2>Gradebook for Maths</h2><p>You do not have access.</p><p>WithPermission(Gradebook)</p></div>
Key points
- A HOC is a function from a component to an enhanced component.
- Forward all props, set a display name and never create a HOC during render.
- Drawbacks: wrapper nesting, prop name collisions and hidden data sources.
- Custom hooks are the modern way to share stateful logic.
- HOCs remain useful for gates that decide whether to render.
Common mistakes
- Creating a HOC inside a component, which remounts it on every render.
- Not forwarding props to the wrapped component.
- Forgetting displayName, which makes DevTools output unreadable.
- Letting two HOCs inject the same prop name.
- Using a HOC for logic that a custom hook handles more clearly.
Follow-ups
- How would you rewrite withPermission as a hook or a small gate component?
- Why does calling a HOC inside render lose state?
- How do HOCs interact with refs in React 19?
Tips
- Label it legacy, but show you can still write one safely.
- Name the three problems (nesting, collisions, hidden source). It shows you understand why hooks won.
- Mention that gate wrappers are the case where HOCs still fit.