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.

[Flatlist] getItemLayout fires multiple times upon every frame scrolled manually

See original GitHub issue

Environment

Environment: OS: macOS High Sierra 10.13.6 Node: 8.9.4 Yarn: 1.3.2 npm: 5.5.1 Watchman: 4.9.0 Xcode: Xcode 9.4.1 Build version 9F2000 Android Studio: 3.0 AI-171.4443003

Packages: (wanted => installed) react: 16.4.0 => 16.4.0 react-native: ^0.55.4 => 0.55.4

iOS

Description

Without getItemLayout flatlist works as expected - fast. With getItemLayout we started noticing rows aren’t loading instantly when scrolling manually. After removing getItemLayout performance is good again.

We are using shouldComponentUpdate (also tried PureComponent).

In the gif you can see logs that getItemLayout is firing constantly when using flatlist manually - which seems to be why performance is suffering. Is this to be expected?

ezgif com-video-to-gif

Reproducible Demo

Simplified my use-case with reusing part of someone else’s demo 😃

class MyListItem extends React.PureComponent {
    render() {
        return (
            <View
                style={{
                    paddingVertical: 10,
                }}>
                <TouchableOpacity onPress={() => null}>
                    <Text
                        style={{
                            color: '#000',
                            height: 40,
                            justifyContent: 'center'
                        }}>
                        lalalalaal
                      </Text>
                </TouchableOpacity>
            </View>
        )
    }
}

_renderItem = ({ item }) => (
    <MyListItem
        produto={item.produto}
    />
);

render() {
                    return <FlatList
                    data={['one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three']}
                    showsVerticalScrollIndicator={false}
                    initialListSize={4}
                    renderItem={_renderItem}
                    keyExtractor={(item, index) => '' + index}
                    ref={(ref) => { this.flatListRef = ref }}
                    getItemLayout={(data, index) => {
                        console.log("get item layout " + index)
                        return { length: 40, offset: 40 * index, index }
                    }}
                />
}

Expected behavior

getItemLayout only fires when scrollToIndex or similar methods are fired. Or at most when rows are actually re-rendering - though it doesn’t look like it’s used when simply rendering visible rows.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

3reactions
srameshrcommented, Aug 10, 2018
3reactions
newahcommented, Aug 10, 2018

@react-native-bot tested with 0.56. Issues still exists.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizing Flatlist Configuration - React Native
This controls the amount of items rendered per batch, which is the next chunk of items rendered on every scroll. Pros: Setting a...
Read more >
Flatlist getItemLayout usecase - Stack Overflow
React Native FlatList optimises list view performance by only rendering rows that are currently visible on the screen, and unmounting rows ...
Read more >
you specified `onscroll` on a <scrollview> but not ...
Just to learn something new, the scrollEventThrottle prop defines how many times the onScroll event will be fired while you scrolling. The bigger...
Read more >
A deep dive into React Native FlatList - LogRocket Blog
FlatList is a React Native component that allows you to render lists ... doesn't have to manually scroll to the bottom, thus saving...
Read more >
Usage | FlashList
Switch from FlatList to FlashList and render the list once. You should see a warning about missing estimatedItemSize and a suggestion. Set this ......
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