Proposal: switch from HoC to render prop (or function as children)
See original GitHub issueThinking about the possibility of replacing the HoC technique with the render prop technique.
The syntax would go from:
const MyVirtualList = VirtualList()(MyList);
return (
<MyVirtualList
items={myBigListOfItems}
itemHeight={100}
/>
);
to
return (
<VirtualList
items={myBigListOfItems}
itemHeight={itemHeight}
renderVirtual={({ items, style} => (
<ul style={style}>
{items.map(item => (
<li key={`item_${item.id}`} style={{height: itemHeight}}>
Lorem ipsum dolor sit amet
</li>
))}
</ul>
))}
/>
);
Alternatively it could be function as children:
return (
<VirtualList
items={myBigListOfItems}
itemHeight={itemHeight}
>
{({ items, style } => (
<ul style={style}>
{items.map(item => (
<li key={`item_${item.id}`} style={{height: itemHeight}}>
Lorem ipsum dolor sit amet
</li>
))}
</ul>
))}
</VirtualList>
);
Any thoughts?
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
Higher-Order Components - React
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per...
Read more >Avoiding HOC; Favoring render props - GitHub
In short, a render prop component accepts a render prop, which is a function that's used to render its children. Instead of rendering...
Read more >React: Do Hooks Replace HOCs and Render Props?
One common job of a HOC/render prop is to manage the lifecycle of some data, and pass that data to the derived component...
Read more >Higher-Order Components in React Hooks era - Developer way
The component that you pass into the HOC (CountriesWithFormId) doesn't re-render since its memoized inside. And the first function just extracts ...
Read more >Function as Child Components Are an Anti-Pattern
A Function as Child Component (or FaCC) is a pattern that lets you you pass a render function to a component as the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Based on my real world experience, here are my thoughts:
Children props approach is the cleanest in terms of the component API - but it’s also the quirkiest because children is an opaque data structure. That doesn’t necessarily mean it’s going to be harder than others - that solely depends on how we use the children prop - but in terms of flexibility, it does introduce constraints (we can only do what React lets us).
render props - its looks slightly convoluted compared to children props - but since it’s just a simple array, it gives us lots of flexibility on how we want to manipulate/use it.
Between render props and HOC, with the example given in OP, I think HOC is cleaner (code-wise). It’s well understood and works and there are no downsides as far as I can tell.
So I’d vote for children props if its easy enough to support, otherwise keep the current HOC implementation.
Last but not the least, I’m thankful to @developerdizzle for counting me as a contributor, even though I’ve had a very small contribution and I’m not innately familiar with the code (hence my uncertainty about using children prop support).
Update on this - branch is in the works https://github.com/developerdizzle/react-virtual-list/tree/function-as-children