Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-rendering issue in horizontal flatlist #35542

Closed
Ahmedhamed77 opened this issue Dec 2, 2022 · 4 comments
Closed

Re-rendering issue in horizontal flatlist #35542

Ahmedhamed77 opened this issue Dec 2, 2022 · 4 comments
Labels
Component: FlatList Needs: Triage 🔍 Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@Ahmedhamed77
Copy link

Description

The issue is FlatList component gets rendered many times if the data. length is greater than 15

  const renderItem = () => {
    console.log('renderItem');
    return (
      <View
        style={{
          width: 200,
          height: 100,
          backgroundColor: 'red',
          margin: 20,
        }}
      />
    );
  };

<FlatList
        horizontal
        renderItem={renderItem}
        data={[1, 2, 3, 4, 5, 6, 7, 8, 9,10,12,13,14,15]}
        keyExtractor={item => item.toString()}
      />

this flatList rendered 25 times, even the data.length is just 15

Version

^0.67.4

Output of npx react-native info

OS: macOS 12.5.1
CPU: (8) arm64 Apple M1
Memory: 117.08 MB / 8.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.9.0 - /opt/homebrew/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 8.19.1 - /opt/homebrew/bin/npm
Watchman: 2022.09.05.00 - /opt/homebrew/bin/watchman
Managers:
CocoaPods: 1.11.3 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 21.4, iOS 16.0, macOS 12.3, tvOS 16.0, watchOS 9.0
Android SDK: Not Found
IDEs:
Android Studio: 2020.3 AI-203.7717.56.2031.7678000
Xcode: 14.0.1/14A400 - /usr/bin/xcodebuild
Languages:
Java: 18.0.1.1 - /usr/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 17.0.2 => 17.0.2
react-native: ^0.67.4 => 0.67.4
react-native-macos: Not Found
npmGlobalPackages:
react-native: Not Found

Steps to reproduce

just write this flatList in your app.js

Snack, code example, screenshot, or link to a repository

Screenshot 2022-12-02 at 14 51 27

Screenshot 2022-12-02 at 14 53 32

@KarthikMAM
Copy link

KarthikMAM commented Dec 2, 2022

That is expected. Flat list renders the list in batches until the windowSize has been fully rendered

initialNumtoRender is 10. https://reactnative.dev/docs/optimizing-flatlist-configuration#initialnumtorender

So, it will first add 10 items and then add x more items to the list based on https://reactnative.dev/docs/optimizing-flatlist-configuration#maxtorenderperbatch

When the new batch is added... the list is looped and renderItem is called for all the items in the list.

I think even during the scroll... renderItem will get called for all the items, when additional items outside of the window come into the window.

So, I would say this is expected and you have to a memoized item in the result of renderItem.

@Ahmedhamed77
Copy link
Author

So, I would say this is expected and you have to a memoized item in the result of renderItem.

could you give an example of what you are saying because I don't see the logic of what you are saying?
so could you show how to memorize item in the result of renderItem.?

@github-actions
Copy link

github-actions bot commented Jul 6, 2023

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jul 6, 2023
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: FlatList Needs: Triage 🔍 Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

3 participants