首页 > 解决方案 > React 忽略 shouldComponentUpdate() 中的 props 更改

问题描述

有时对 FlatList 项目使用 PureComponent 或 Memo 有助于优化 flatlist 性能,但有时也会导致更多性能问题。这取决于传递给列表项的道具。如果将太多道具传递给列表项,则只会使性能变差。

在我的用例中,列表的主要组件有很多属性,所以我需要实现自己的 shouldComponentUpdate 函数。由于我的道具不会导致重新渲染(更新),我需要在 shouldComponentUpdate 中忽略它们(但不是状态)。

我尝试过这样的事情:

 state = {
    count: 0,
    player: {
       name: "Nick"
    }
 } 


 shouldComponentUpdate(nextProps, nextState) {
    return nextState !== this.state;
 }

但不起作用,因为 nextState 引用了与 this.state 相同的内存(我认为)。

任何想法如何做到这一点?

这样做因为我遵循这些提示以获得更好的 FlatList 性能)。这是我的平面列表:

 keyExtractor = ({ id }) => id; // each element in the data array has an unique uuid.

 <FlatList
    data={data}
    getItemLayout={this.getItemLayout}
    keyExtractor={this.keyExtractor}
    listKey={listKey}
    legacyImplementation={false}
    numColumns={1}
    renderItem={this.renderItem}
    initialNumToRender={10}
    windowSize={5}
    maxToRenderPerBatch={5}
    updateCellsBatchingPeriod={50}
    removeClippedSubviews={false}
    ListFooterComponent={this.renderFooter}
    ListEmptyComponent={ListEmptyComponent}
    onEndReached={onEndReached}
    onEndReachedThreshold={0.15}
  />

谢谢你。

标签: javascriptreactjsperformancereact-native

解决方案


推荐阅读