Render Props vs HOCs for Logic Sharing
What you'll learn: How render props and higher-order components compare as patterns for sharing logic, and when to choose each approach.
Both render props and HOCs solve the same problem—sharing stateful logic across components—but they take different philosophical approaches.
The Core Difference
HOCs (Higher-Order Components) wrap your component in another component, adding props automatically. They're implicit: you don't see the data flow in JSX, just export default withAuth(MyComponent). Multiple HOCs stack vertically, creating wrapper layers (sometimes called "wrapper hell").
Render props make data flow explicit. You see exactly what data flows from parent to child right in your JSX: <Mouse render={(x, y) => ...} />. The logic provider calls your function with the data, and you decide what to render.
When Each Shines
Render props excel when:
- You want explicit, visible data flow
- You need to avoid deeply nested wrappers
- Consuming different data in different parts of your tree
- You want immediate clarity about what props come from where
HOCs work well when:
- Composing multiple behaviors tersely (
withAuth(withRouter(withTheme(Component)))) - The same enhancement applies uniformly across many components
- You want separation of concerns at the file level
The Trade-off
Render props avoid the "wrapper hell" you get with many HOCs, but can create deeply indented JSX if you nest multiple render props. HOCs keep JSX clean but hide the data source. Neither is "better"—they're tools for different situations.
Key Takeaway: Render props make logic sharing explicit and visible in your JSX, while HOCs abstract it away for terser composition—choose based on whether you value explicitness or brevity for that specific case.