DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. What is Higher-Order Component (HOC) in React?
XLinkedInReddit
MediumFrontend Engineering

What is Higher-Order Component (HOC) in React?

D
DevPrep Team
February 9, 2026·2 min read·0
Table of Contents
  • What is a HOC?
  • Real-World Examples
  • Authentication HOC
  • Analytics HOC
  • Problems with HOCs
  • HOCs vs Hooks (Modern Approach)
  • When HOCs Still Make Sense
  • Summary

By Rahul — Google Frontend Engineer

What is a HOC?

A Higher-Order Component is a function that takes a component and returns a new component with added behavior. It is a pattern, not a React API.

// A HOC that adds loading state
function withLoading(WrappedComponent) {
  return function WithLoadingComponent({ isLoading, ...props }) {
    if (isLoading) return <Spinner />;
    return <WrappedComponent {...props} />;
  };
}

// Usage
const UserListWithLoading = withLoading(UserList);
<UserListWithLoading isLoading={loading} users={users} />

Real-World Examples

Authentication HOC

function withAuth(WrappedComponent) {
  return function AuthComponent(props) {
    const { user, loading } = useAuth();
    
    if (loading) return <Spinner />;
    if (!user) return <Redirect to="/login" />;
    
    return <WrappedComponent {...props} user={user} />;
  };
}

const ProtectedDashboard = withAuth(Dashboard);

Analytics HOC

function withTracking(WrappedComponent, pageName) {
  return function TrackedComponent(props) {
    useEffect(() => {
      analytics.track('page_view', { page: pageName });
    }, []);
    
    return <WrappedComponent {...props} />;
  };
}

const TrackedCheckout = withTracking(Checkout, 'checkout');

Problems with HOCs

  • Wrapper hell: withAuth(withTheme(withLoading(withRouter(Component)))) — deeply nested, hard to debug
  • Prop collisions: Multiple HOCs might inject props with the same name
  • Static methods lost: HOC does not copy static methods from the wrapped component
  • Refs do not pass through: Need React.forwardRef to fix

HOCs vs Hooks (Modern Approach)

// HOC approach (old)
const EnhancedComponent = withAuth(withTheme(MyComponent));

// Hooks approach (modern — preferred)
function MyComponent() {
  const { user } = useAuth();
  const { theme } = useTheme();
  
  if (!user) return <Redirect to="/login" />;
  return <div style={{ color: theme.primary }}>...</div>;
}

When HOCs Still Make Sense

  • Wrapping class components (hooks only work in function components)
  • Adding behavior that wraps the entire render (error boundaries, suspense boundaries)
  • Third-party library integrations that provide HOCs

Summary

HOCs are functions that wrap components to add behavior. They were the primary code reuse pattern before hooks. Today, prefer custom hooks for shared logic. HOCs are still useful for render-wrapping patterns and class component support.

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • What is a HOC?
  • Real-World Examples
  • Authentication HOC
  • Analytics HOC
  • Problems with HOCs
  • HOCs vs Hooks (Modern Approach)
  • When HOCs Still Make Sense
  • Summary

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.