【问题标题】:React Native | Rerender Flatlist反应原生 |重新渲染平面列表
【发布时间】:2021-03-10 02:21:17
【问题描述】:

我有一个列表:数据。然后当我更新列表时,它会再次呈现所有元素。例如,假设有 10 个项目。其中之一发生了变化。 10 个项目再次渲染。但由于其中 9 个是相同的,因此不应再次渲染。我该怎么办?

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

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

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

【问题讨论】:

  • (&lt;View key={item.key}&gt;&lt;Text&gt;{item.title}&lt;/Text&gt;&lt;/View&gt;) - 使用key 字段解决此问题?不确定....只是一个想法
  • 感谢您的回答。它不起作用。

标签: reactjs react-native react-native-flatlist


【解决方案1】:

你必须使用 extraDataFlatlist 属性

根据文档:

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.

或者您可以在 renderitem 组件中实现 shouldComponentUpdate 并在您想要渲染组件时进行检查。

【讨论】:

  • 感谢您的回答。对于这个例子,假设 item.name 已经改变,你会写 extraData 吗?
  • 那我去shouldComponentUpdate
猜你喜欢
  • 2022-10-17
  • 1970-01-01
  • 2019-08-09
  • 2018-01-07
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 2021-08-31
相关资源
最近更新 更多