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.

data param on itemSize func for VariableSizeList

See original GitHub issue

Thanks for adding the data param on the itemKey func in 3c9a558.

I use the itemSize function in my usage of VariableSizeList, where the size of the item depends on the data in some way. It would be useful if the itemSize function also had a data param.

I can send a PR if you like, just wanted to run it by you first. Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
jancama2commented, Feb 26, 2019

Here is a simplified example:

<VariableSizeList
  height={totalHeight}
  itemData={items}
  itemCount={items.length}
  itemSize={(index) => items[index].itemType === 'header' ? 32 : 64 }
  itemKey={(index, data) => data[index].key }
  >
  {ItemRenderer}
</VariableSizeList>

class ItemRenderer extends React.PureComponent {
  render() {
    const item = this.props.data[this.props.index]

    return (
      <div style={this.props.style}>
        {
          item.itemType === 'header'
          ? <h2>{item.headerText}</h2>
          : <div>{item.otherStuff}</div>
        }
      </div>
    )
  }
}

The list of items is long (say, hundreds long), and they are grouped into a few sections, each with a header for that section. The section header is smaller in height than the item itself, so the item size is variable.

The above works fine. Just I was able to add the data param for itemKey, and as you can see it would be equally useful on itemSize.

@mj1856 Is this solution working for you even if the items are changing over time (adding, removing)? I have exactly the same type of list and I have an issue with itemSize being called only once when an item is rendered for the first time.

0reactions
pastinepolentacommented, Sep 23, 2020

@bvaughn being new to the library I was also surprised by the lack of data prop (or just the item/row prop) as parameter for this function. I read your opinion on the matter and I just have a question if you might have time to clarify what is the best approach.

My surprise comes from the fact that this forces the usage of a closure like so:

itemSize={ index => {
    const myItem = myItems[index];
    // ... determine the size based on myItem
}}

As mentioned by others this makes the function less “movable” because it relies on and external variable myItems, but perhaps this is not such a strong point to complexify the library?

Also, I am always worried (perhaps mistakenly, please advise) that a closure like so is redefined at each render. That causes a new function to get passed down as props and this I thought is considered a bad practice because it causes unnecessary re-renders of the child component.

Is the solution in this case to memoize the closure with a useCallback ? Is this optimization not so necessary? Having more parameters in the library will solve this, but will perhaps just move the issue on another place causing a perf loss anyway?

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

(react-window) How to pass props to {Row} in <FixedSizeList ...
You can access both parameters from the function definition. So then you can pass the data directly into the Row() call. const Example...
Read more >
Virtualize large lists with react-window - web.dev
Use the VariableSizeList component to render a list of items that have ... The item size function passed to the itemSize prop randomizes...
Read more >
Virtualizing Large Data Lists with react-window
In this tutorial, we'll cover on how render large amount of data, ... and itemSize which takes in a function in this case...
Read more >
How Does Reactwindow Variablesizelist Predetermine ...
I use the itemSize function in my usage of VariableSizeList where the size of the item depends on the data in some way.It...
Read more >
react-window | Yarn - Package Manager
React components for efficiently rendering large lists and tabular data. React window works by only rendering part of a large data set (just...
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