[英]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.