Lesson 25 of 80
A function that takes a component and returns a new, enhanced component — the higher-order-function idea from the JavaScript track, applied to components instead of plain functions.
11 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
A HOC injects a `user` prop, but the component using it is also passed its own `user` prop by its caller. What happens?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A Higher-Order Component is a function: Component -> EnhancedComponent. It wraps the component you give it in another component that adds behavior — props, state, a subscription — before rendering the original:
function withUser(Wrapped) {
return function WithUser(props) {
const user = useContext(UserContext);
return <Wrapped {...props} user={user} />; // inject a prop, render the original
};
}
const ProfileWithUser = withUser(Profile);
<ProfileWithUser /> // Profile receives a `user` prop it never had to fetch itself
This is the higher-order-function concept from the JavaScript track, one level up: instead of taking/returning plain functions, withUser takes/returns components (which, remember, are just functions themselves).
withUser(Profile) — a function that takes a component and returns an enhanced one.
displayName set manually for good DevTools debugging, and needs to explicitly forward any static methods the original component had — none of that happens for free.withUser injects a user prop and the caller also tries to pass its own user prop, one silently overwrites the other depending on prop-spread order — a class of bug custom hooks avoid entirely by not touching props at all."What's a Higher-Order Component, and why would you use a custom hook instead today?" checks the same historical fluency as render props — recognizing the pattern in legacy code and articulating precisely why hooks are the modern replacement.