简体   繁体   English

在功能组件中使用 React.memo 来渲染 Item FlatList 以最小化重新渲染现有项目

[英]using React.memo in functional component to renderItem FlatList to minimize re-render existing items

I'm using functional components, and using Flatlist to render list of datas, it's working ok, but each time state get additional data it always re-rendering existing and it will cause performance problem, I have read these articles from SO but still has no clues我正在使用功能组件,并使用 Flatlist 来呈现数据列表,它工作正常,但每次 state 获取额外数据时,它总是重新呈现现有数据,这会导致性能问题,我已经从 SO 阅读了这些文章,但仍然有没有线索

here is my code that using main Flatlist这是我使用主 Flatlist 的代码

<FlatList
  horizontal={false}
  showsHorizontalScrollIndicator={false}
  data={users}
  keyExtractor={(item, index) => String(index)}
  renderItem={RenderUser}
  onEndReachedThreshold={0.7}
  onEndReached={callBackMoreData}
/>

and here is working RenderUser but the problem it re render existing item if state has additional data, what I would like to achieve is only render additional data这里正在工作 RenderUser 但如果 state 有额外的数据,它会重新呈现现有项目的问题,我想要实现的只是呈现额外的数据

import React from 'react';
import { ListItem } from 'react-native-elements';

const RenderUser = ({ item, index }) => {

  return (
    <React.Fragment>
      { console.log('index: ', index)}
      <ListItem 
        title={item.attributes.name}
      />
    </React.Fragment>
  );
};

export default RenderUser;

and I did tried using this code below (but I get an error message saying TypeError: renderItem is not a function. (in 'renderItem(props)', 'renderItem' is an instance of object))我确实尝试使用下面的代码(但我收到一条错误消息,说 TypeError: renderItem is not a function. (in 'renderItem(props)', 'renderItem' is an instance of object))

import React, { memo } from 'react';
import { ListItem } from 'react-native-elements';

const RenderUser = ({ item, index }) => {

  return (
    <React.Fragment>
      { console.log('index: ', index)}
      <ListItem 
        title={item.attributes.name}
      />
    </React.Fragment>
  );
};

export default memo(RenderUser);

According to react official documentation:根据 React 官方文档:

By default it will only shallowly compare complex objects in the props object. If you want control over the comparison, you can also provide a custom comparison function as the second argument默认情况下,它只会浅比较道具 object 中的复杂对象。如果你想控制比较,你也可以提供自定义比较 function 作为第二个参数

function MyComponent(props) {
  /* render using props */
}
function areEqual(prevProps, nextProps) {
  /*
  return true if passing nextProps to render would return
  the same result as passing prevProps to render,
  otherwise return false
  */
}
export default React.memo(MyComponent, areEqual);

In your case you need to add condtion:在您的情况下,您需要添加条件:

import React, { memo } from 'react';
import { ListItem } from 'react-native-elements';

const RenderUser = ({ item, index }) => {

  return (
    <React.Fragment>
      { console.log('index: ', index)}
      <ListItem 
        title={item.attributes.name}
      />
    </React.Fragment>
  );
};

function arePropsEqual(prevProps, nextProps) {
      /*
      return true if passing nextProps to render would return
      the same result as passing prevProps to render,
      otherwise return false
      */
      return nextProps.item.attribute.name===prevProps.item.attribute.name
    }

export default memo(RenderUser,arePropsEqual);

Note : Not sure how many props you are getting and what are unique.注意:不确定您获得了多少道具以及哪些是独特的。 Basically you need to compare that last one is equal to this one return true in that way react does not re-render your component基本上你需要比较最后一个等于这个返回真这样反应不会重新渲染你的组件

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

相关问题 使用 React.memo 和 React.useCallback 防止重新渲染 - Prevent re-render using React.memo and React.useCallback FlatList 不在功能组件中重新渲染? - FlatList not re-render in a functional component? 为什么在 React 中,当父组件重新渲染时子组件不重新渲染(子组件未被 React.memo 包装)? - Why, in React, do children not re-render when parent component re-renders(children are not wrapped by React.memo)? 为什么双花括号的子代会导致我的React.memo组件重新渲染,即使它们的值相同? - Why do double curly braced children cause my React.memo component to re-render even though the values are the same? React i18next - 更改语言不会重新渲染使用 React.memo 记忆的组件 - React i18next - Change language does not re-render components that are memoized using React.memo React.memo() 与 React 功能组件 - React.memo() vs React functional component 功能组件仍然使用 react.memo 重新渲染 - functional component still rerender with react.memo 带有 React.memo() 的功能组件仍然会重新渲染 - Functional component with React.memo() still rerenders React.memo 不适用于功能组件 - React.memo not working on a functional component 功能组件中的 React.memo 和失效不会失效 - React.memo and invalidation in Functional component not invalidating
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM