question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: switch from HoC to render prop (or function as children)

See original GitHub issue

Thinking 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:open
  • Created 5 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
mrchiefcommented, Apr 24, 2018

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).

0reactions
developerdizzlecommented, Nov 15, 2018
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found