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.

VirtualizedList + getItemLayout only renders initialNumToRender items

See original GitHub issue

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

  1. react-native -v: 0.47.2
  2. node -v: v6.10.3
  3. npm -v: 3.10.10
  4. yarn --version: n/a

Then, specify:

  • Target Platform: iOS
  • Development Operating System: macOS
  • Build tools: Xcode

Steps to Reproduce

(Write your steps here:)

  1. Create a component with a FlatList w/t getItemLayout, i.e.
<FlatList
            ref={ref => { const anyRef: any = ref; this._flatList = anyRef }}
            data={data}
            renderItem={this._renderItem}
            onViewableItemsChanged={this._onViewableItemsChanged}
            getItemLayout={this._getItemLayout}
            initialNumToRender={10}
          />

I am using the standard getItemLayout:

getItemLayout={(data, index) => (
  {length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index}
)}
  1. Provide a data source that has more than initialNumToRender items
  2. Render and scroll to bottom

Expected Behavior

(Write what you thought would happen.)

All items are rendered.

Actual Behavior

Only initialNumToRender items are rendered.

(Write what happened. Add screenshots!)

Reproducible Demo

The bug is in VirtualizedList:

const onLayout =
      getItemLayout && !parentProps.debug && !fillRateHelper.enabled()
        ? undefined
        : this.props.onLayout;

The fix is:

const onLayout = this.props.onLayout;

You need onLayout because somebody needs to call _onCellLayout(e, key, ii).

(Paste the link to an example project and exact instructions to reproduce the issue.)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:21
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

20reactions
PublicParadisecommented, Sep 19, 2017

After working with getItemLayout plus my proposed fix for a few weeks I have come to the conclusion that getItemLayout cannot be used in production code.

Here are my recommendations:

  1. Avoid getItemLayout. It’s not working correctly. I would classify the quality of the code as experimental at best.
  2. Avoid scrollToIndex, scrollToItem, and scrollToEnd , because they require getItemLayout. Instead use scrollToOffset, which comes with fewer bugs.
  3. Consider changing the documentation and let clients know about the experimental status of getItemLayout. Something along the lines of the removeClippedSubviews warning:

Note: may have bugs (missing content) in some circumstances - use at your own risk.

  1. Please do something about the bug triage turn around times.

Good luck, I am closing this issue.

12reactions
Elijencommented, Jan 11, 2018

Why is this issue closed? If getItemLayout is not working correctly the issue should stay open.

Read more comments on GitHub >

github_iconTop Results From Across the Web

VirtualizedList - React Native
This disables the "scroll to top" optimization that keeps the first initialNumToRender items always rendered and immediately renders the items ...
Read more >
VirtualizedList: You have a large list that is slow to update ...
In this case only 8 items, and it doesn't seem like they are retendered but somehow the warning keep showing up. I also...
Read more >
8 ways to optimize React native FlatList performance
Use getItemLayout to optimize flatlist react native ... For example initialNumToRender to render less number of items so that first render ...
Read more >
Display a List Using the FlatList Component in React Native
React Native provides a FlatList component to create a list. FlatList only renders the list items that can be displayed on the screen....
Read more >
react native flatlist infinite loop - You.com | The AI Search ...
I thought the FlatList would only re-render when the data or extraData props ... VirtualizedList + getItemLayout only renders initialNumToRender items#5652.
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