Read the instructions and examples in this document.
Apply the patterns to your implementation, adapting to your specific context.
Verify your implementation against the details and edge cases listed above.
Harness Integration
Type: knowledge — this skill is a reference document, not a procedural workflow.
No tools or state — consumed as context by other skills and agents.
Success Criteria
The patterns described in this document are applied correctly in the implementation.
Edge cases and anti-patterns listed in this document are avoided.
1---2name: react-hoc-pattern3description: React HOC Pattern4---5# React HOC Pattern67> Extend component behavior by wrapping in a higher-order component89## When to Use1011- Adding cross-cutting concerns (logging, authentication gating, analytics) to multiple components without modifying each12- Working with class components that cannot use hooks13- Integrating with third-party libraries that use HOC APIs (Redux `connect`, React Router `withRouter`)14- You want to enhance a component's props without the consumer being aware of the enhancement1516## Instructions17181. Create a function that accepts a component and returns a new component.192. The wrapper component renders the wrapped component, forwarding all props.203. Add the enhancement (extra props, lifecycle behavior, conditional rendering) in the wrapper.214. Name the HOC `with<Behavior>` by convention.225. Forward refs using `React.forwardRef` if the wrapped component uses refs.236. Set `displayName` on the HOC result for debugging: `WrappedComponent.displayName = \`withAuth(\${Component.displayName})\``.2425```typescript26function withAuthentication<P extends object>(27 WrappedComponent: React.ComponentType<P>28) {29 const WithAuth = (props: P) => {30 const { isAuthenticated } = useAuth();31 if (!isAuthenticated) return <Redirect to="/login" />;32 return <WrappedComponent {...props} />;33 };34 WithAuth.displayName = `withAuthentication(${WrappedComponent.displayName ?? WrappedComponent.name})`;35 return WithAuth;36}37```3839## Details4041HOCs are a functional composition pattern borrowed from functional programming. They were the primary code-reuse mechanism before hooks.4243**When to prefer hooks over HOCs:**4445- Hooks are simpler, avoid prop name collisions, and are easier to type in TypeScript46- "Wrapper hell" — stacking multiple HOCs creates deeply nested component trees in DevTools47- HOC prop injection can clash if two HOCs inject the same prop name4849**Valid HOC use cases in modern React:**5051- HOC-based library APIs (Redux, styled-components `withTheme`)52- Class components that cannot use hooks53- Performance optimization with `React.memo` and custom comparison5455**TypeScript note:** HOC generic typing requires careful handling of `ComponentProps` and `Omit` to correctly type the enhanced component's props.5657## Source5859https://patterns.dev/react/hoc-pattern6061## Process62631. Read the instructions and examples in this document.642. Apply the patterns to your implementation, adapting to your specific context.653. Verify your implementation against the details and edge cases listed above.6667## Harness Integration6869- **Type:** knowledge — this skill is a reference document, not a procedural workflow.70- **No tools or state** — consumed as context by other skills and agents.7172## Success Criteria7374- The patterns described in this document are applied correctly in the implementation.75- Edge cases and anti-patterns listed in this document are avoided.
Run npx skillmds@latest add intense-visions/react-hoc-pattern in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
React HOC Pattern It is listed under Web & Frontend on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Intense-Visions (@intense-visions) published this skill. Their other Agent Skills are listed on their SkillMD profile.