Higher-Order Component
In one line
A higher-order component is a function that takes a component and returns a new enhanced component — a pattern for sharing logic.
In simple words
A higher-order component (HOC) is a function Component => Component. It takes a component and returns a new one with added behavior. It's a pattern inspired by higher-order functions in functional programming.
const EnhancedComponent = withAuth(MyComponent) — withAuth is the HOC. It might check if the user is logged in and either render MyComponent or redirect to login.
HOCs were the primary way to share logic before Hooks. Today, custom Hooks are preferred because they are simpler, don't add nesting to the component tree, and are easier to type with TypeScript. HOCs are still seen in older codebases and some libraries (like React Redux's connect).
Example
function withLoading(Component) {
return function WrappedComponent({ isLoading, ...props }) {
if (isLoading) return <Spinner />;
return <Component {...props} />;
};
}
const MyComponentWithLoading = withLoading(MyComponent);withLoading wraps any component and shows a spinner while isLoading is true, otherwise renders the original component with the remaining props.
Related terms
Related Resources
Related Lessons
- React Best Practices
Learn HOCs and modern alternatives.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.