react-native – 基本FlatList代码抛出警告 – React Native

前端之家收集整理的这篇文章主要介绍了react-native – 基本FlatList代码抛出警告 – React Native前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
FlatList似乎不起作用.我收到了这个警告.

VirtualizedList:缺少项目的键,确保在每个项目上指定键属性或提供自定义keyExtractor.

码:

  1. <FlatList
  2. data={[{name: 'a'},{name: 'b'}]}
  3. renderItem={
  4. (item) => <Text key={Math.random().toString()}>{item.name}</Text>
  5. }
  6. key={Math.random().toString()} />
只需这样做:
  1. <FlatList
  2. data={[{name: 'a'},{name: 'b'}]}
  3. renderItem={
  4. (item) => <Text>{item.name}</Text>
  5. }
  6. keyExtractor={(item,index) => index}
  7. />

资料来源:here

猜你在找的React相关文章