What problem does it solve?
HOCs (Higher-Order Components) solve the problem of duplicating shared React component logic across many components by centralizing that behavior in one reusable wrapper.
Core Features & Use Cases
- Cross-cutting logic reuse: Share common behavior such as authorization, logging, styling, or data fetching across multiple components without rewriting each component.
- Enhanced component outputs: Wrap a component to add props and conditional rendering (e.g., show Loading… until data arrives).
- Composition with care: Build up multiple behaviors by composing HOCs while managing prop-name collisions and composition order.
- When to prefer hooks: Use HOCs primarily when shared behavior is uncustomized across many components, and switch to hooks when per-component customization or improved readability matters.
Quick Start
Create a HOC function that takes a component, adds the shared behavior (such as loading state or extra props), and exports the wrapped component for use in your React tree.