简体   繁体   English

如何在 React Native 中优化 FlatList

[英]How to optimize FlatList in React Native

Can you please tell me how can I optimize this flatlist in react native.你能告诉我如何在 React Native 中优化这个平面列表吗? I mean how can I do that app will render not the whole list of data but just small part of it for example 10 items, and then when the user will scroll it down it will load more of data from list?我的意思是我该怎么做该应用程序将不呈现整个数据列表,而只呈现其中的一小部分,例如 10 个项目,然后当用户向下滚动它时,它将从列表中加载更多数据?

that's the code这就是代码

import React, {useState, useEffect} from 'react';
import {
  SafeAreaView,
  StatusBar,
  StyleSheet,
  Text,
  View,
  FlatList,
  TextInput,
} from 'react-native';
import {newdata} from '../Data/newdata';

const Sample = () => {
  const DATA = newdata;
  const [searchText, onChangeSearch] = useState('');
  const [filteredData, setFilteredData] = useState([]);

  useEffect(() => {
    const filtered = DATA.filter(item =>
      item.title.toLowerCase().includes(searchText.toLowerCase()),
    );
    if (searchText === '') {
      return setFilteredData(DATA);
    }

    setFilteredData(filtered);
  }, [searchText]);

  const Item = ({title}) => (
    <View style={styles.item}>
      <Text style={styles.title}>{title}</Text>
    </View>
  );

  const renderItem = ({item}) => <Item title={item.title} />;

  return (
    <SafeAreaView style={styles.container}>
      <TextInput
        style={{
          height: 50,
          borderColor: '#919191',
          borderWidth: 1,
          margin: 10,
          paddingLeft: 15,
          borderRadius: 10,
        }}
        onChangeText={newText => onChangeSearch(newText)}
        placeholder="Axtaris..."
      />
      <FlatList
        data={filteredData}
        renderItem={renderItem}
        keyExtractor={(item, index) => item.key}
      />
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: StatusBar.currentHeight || 0,
    marginBottom: 75,
  },
  item: {
    backgroundColor: '#ededed',
    padding: 20,
    marginVertical: 2,
    marginHorizontal: 10,
    borderRadius: 20,
  },
  title: {
    fontSize: 20,
  },
});

export default Sample;

PS newdata has about 42000 of items, and app running very slow. PS newdata 有大约42000 项,应用程序运行很慢。 That is the screenshot of app这是app的截图应用截图

You can easily achieve that by using the initialNumToRender prop in FlatList component您可以通过在 FlatList 组件中使用initialNumToRender道具轻松实现这一点

How many items to render in the initial batch.初始批次中要渲染的项目数。 This should be enough to fill the screen but not much more.这应该足以填满屏幕,但不会更多。 Note these items will never be unmounted as part of the windowed rendering in order to improve perceived performance of scroll-to-top actions.请注意,这些项目永远不会作为窗口渲染的一部分被卸载,以提高滚动到顶部操作的感知性能。

  <FlatList
    data={filteredData}
    renderItem={renderItem}
    keyExtractor={item => item.key}
    initialNumToRender={10}
  />

Ali,阿里,

You have some features inside Flatlist in order to optimize it for example: Flatlist 中有一些功能可以优化它,例如:

maxtorenderperbatch : This controls the amount of items rendered per batch, which is the next chunk of items rendered on every scroll. maxtorenderperbatch :这控制每批渲染的项目数量,这是在每个滚动上渲染的下一个项目块。 Read more here在这里阅读更多

initialNumToRender : Define precise number of items that would cover the screen for every device. initialNumToRender :定义将覆盖每个设备屏幕的项目的精确数量。 [Read more here][1] [在这里阅读更多][1]

Also, you can use Infinite Scroll, is very useful instead of render the whole list you can render only amount of items, and when the user is scrolling to the end the app load more items.此外,您可以使用 Infinite Scroll,它非常有用,而不是渲染整个列表,您可以只渲染一定数量的项目,当用户滚动到最后时,应用程序会加载更多项目。

onEndReached : Called once when the scroll position gets within onEndReachedThreshold of the rendered content. onEndReached :当滚动 position 进入渲染内容的 onEndReachedThreshold 时调用一次。

onEndReachedThreshold : How far from the end (in units of visible length of the list) the bottom edge of the list must be from the end of the content to trigger the onEndReached callback. onEndReachedThreshold :列表的底边距离末尾的距离(以列表的可见长度为单位)必须距离内容的末尾多远才能触发 onEndReached 回调。

Here is an example how to use it.这是一个如何使用它的例子。

<FlatList
    data={filteredData}
    renderItem={renderItem}
    keyExtractor={(item, index) => item.key}

    onEndReached={ loadMoreItems }
    onEndReachedThreshold={ 0.5 }
    maxToRenderPerBatch={3}
    initialNumToRender={5}

  />

loadMoreItems:加载更多项目:

const loadMoreItems = ( ) => {
        
       // Here you logic to render more items, when user scroll to the end
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM