React Native | Rerender Flatlist

烂漫一生 提交于 2021-01-28 04:49:45

问题


I have a list: data. Then when I update the list, it renders all the elements again. For example, let's say there are 10 items. One of these has changed. 10 items are rendering again. But since 9 of them are the same, they should not be rendered again. How can I do?

const Item =({item}) => (
   <Text>{item.name}</Text>
)

const List = () => {
  const [data, setData] = useState([..]);

  return (
     <FlatList
        data={data}
        renderItem = {Item}
     >
)}
export default List;

回答1:


You have to use extraData prop of Flatlist

According to docs:

By passing extraData={this.state} to FlatList we make sure FlatList will re-render itself when the state.selected changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.

Or you can implement shouldComponentUpdate inside your renderitem component and place a check when you wanted to render your component.



来源:https://stackoverflow.com/questions/65034994/react-native-rerender-flatlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!