42

I have a list just simple text that rendering into flatlist on react native but I am experiencing very very slow performance which makes app unusable.

How can I solve this? My code is:

<FlatList
  data={[{key: 'a'}, {key: 'b'} ... +400]}
  renderItem={({item}) => <Text>{item.key}</Text>}
/>
peterh
  • 11,875
  • 18
  • 85
  • 108
user8026867
  • 1,849
  • 3
  • 11
  • 11

7 Answers7

98

Here is my suggestions:

A. Avoid anonymous arrow function on renderItem props.

Move out the renderItem function to the outside of render function, so it won't recreate itself each time render function called.

B. Try add initialNumToRender prop on your FlatList

It will define how many items will be rendered for the first time, it could save some resources with lot of data.

C. Define the key prop on your Item Component

Simply it will avoid re-render on dynamically added/removed items with defined key on each item. Make sure it is unique, don't use index as the key! You can also using keyExtractor as an alternative.

D. Optional optimization

Try use getItemLayout to skip measurement of dynamic content. Also there is some prop called maxToRenderPerBatch, windowSize that you can use to limit how many items you will rendered. Refer to the official doc to VirtualizedList or FlatList.

E. Talk is Cheap, show me the code!

// render item function, outside from class's `render()`
const renderItem = ({ item }) => (<Text key={item.key}>{item.key}</Text>);

// we set the height of item is fixed
const getItemLayout = (data, index) => (
  {length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index}
);

const items = [{ key: 'a' }, { key: 'b'}, ...+400];

function render () => (
  <FlatList
    data={items}
    renderItem={renderItem}
    getItemLayout={getItemLayout}
    initialNumToRender={5}
    maxToRenderPerBatch={10}
    windowSize={10}
  />
);
justcodin
  • 857
  • 7
  • 17
I Putu Yoga Permana
  • 3,980
  • 29
  • 33
8

One of the simple ways to optimize your flatlist is by using React.memo. In technical words, it basically does a shallow comparison of your data and check whether they needs to be re-rendered or not.

Make a file such as ListComponent.js and add the renderItem JSX to it, and and it to the renderItem.

// ListComponent.js
import React, { memo } from "react";
import { StyleSheet, Text, View } from "react-native";

const ListComponent = ({ item }) => {
  return  <View ></View>
};

export default memo(ListComponent);

Here is your FlatList

<FlatList
        data={data}
        removeClippedSubviews={true}
        maxToRenderPerBatch={8}
        windowSize={11}
        initialNumToRender={8}
        keyExtractor={keyExtractor}
        renderItem={({ item }) => (
               <ListComponent item={item} />
          )}
      />
Ayush Kumar
  • 494
  • 1
  • 6
  • 21
7

Try out this listview https://github.com/Flipkart/ReactEssentials, it renders far fewer items than FlatList and then recycles them. Should be much faster.

npm install --save recyclerlistview
naqvitalha
  • 793
  • 5
  • 9
5

check this link

https://github.com/filipemerker/flatlist-performance-tips

Example

FlatList
    containerContentStyle={styles.container}
    data={countries}
    renderItem={({ item }) => (
      <View style={styles.results}>
        <Results 
          {...this.props} 
          country={item} 
          handleUpdate={this.handleUpdate}
          pendingCountry={pendingCountry}
        />
      </View>
    )}
    keyExtractor={item => item.alpha2code}
    ListHeaderComponent={() => this.renderHeader()}

    // Performance settings
    removeClippedSubviews={true} // Unmount components when outside of window 
    initialNumToRender={2} // Reduce initial render amount
    maxToRenderPerBatch={1} // Reduce number in each render batch
    updateCellsBatchingPeriod={100} // Increase time between renders
    windowSize={7} // Reduce the window size
  />
Shivo'ham 0
  • 1,333
  • 11
  • 19
0

Another optimization would be to provide a key using keyExtractor prop. It's very important.

Vivek_Neel
  • 1,343
  • 1
  • 14
  • 25
0

I used 'react-native-optimized-flatlist' and my problem was solved, the only thing to be careful about is that when you use this package, it removes keyExtractor and extraData

Dharman
  • 30,962
  • 25
  • 85
  • 135
-2

You can use react-native-optimized-flatlist. It is the optimized version of Flatlist.

1) Add this package by :

npm i -S react-native-optimized-flatlist

OR

yarn add react-native-optimized-flatlist

2) Replace <FlatList/> by <OptimizedFlatlist/>

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
Khalid Rahman
  • 125
  • 10