React Native - Use a keyExtractor with FlatList

一世执手 提交于 2019-12-03 04:52:08

"VirtualizedList: missing keys for items, make sure to specify a key property on an item or provide a custom keyExtractor"

This is a warning that the elements of the list are missing keys. These unique keys are what allow the VirtualizedList (which is what FlatList is built on) to track items and are really important in terms of efficiency.

You will have to choose a unique key prop, like an id or an email.

The keyExtractor falls back to using the index by default. But the warning will remain visible.

Example : an object defined as {key: doc.id, value: doc.data()} can be used in the extractor as:

keyExtractor={(item, index) => item.key}

Flatlist component should look like that:

<FlatList
  style={{}}
  data={this.state.FeedDataCollection}
  keyExtractor={(item, index) => item.key}
  renderItem={(rowData) =>this.RenderFeedCard(rowData)}
/>

i do this and work for me:

keyExtractor={(item, index) => 'key'+index}

@Sarantis Tofas has the correct answer. Solved it for me! Two additional notes based on the comments:

  1. If you use index then make sure to use index.toString() or you'll get a different warning (Failed child context type - type number supplied to CellRenderer expected string).
  2. FlatList will work with index by default if you don't have key properties or a keyExtractor defined, but it will still throw a warning.

Try using listkey instead of keyExtractor, it worked for me

Best unique key extractor for react native

function looks like:

  keyExtractor = item => {
    return item.id.toString() + new Date().getTime().toString() + (Math.floor(Math.random() * Math.floor(new Date().getTime()))).toString();  };

or without item fields:

  keyExtractor = () => {
    return new Date().getTime().toString() + (Math.floor(Math.random() * Math.floor(new Date().getTime()))).toString();  };

The importance of this error is at the point where you start to change or delete any of the objects of your array because of how your list will get updated if you don't provide that key.

The way React Native deals with a change to your list without a key is to delete everything visible on the screen and then looks at the new array of data and renders a single element for each one.

We don't want React Native to rebuild an entire list just because of one small change to the array of data. Ideally, we want React Native to detect specifically the object we deleted or changed and update accordingly, but that will not happen if we do not provide the key.

The key property allows React Native to track these list of objects and that way it can keep track of what you are modifying or deleting and just delete that particular element with a particular key.

This way the list does not need to be rebuilt from scratch. The key will allow React Native to tie the definition of an object of data with some element that appears on the screen.

Its just allowing React Native to track the different list of objects we are rendering to the screen. Its also a performance optimization on making updates to our list.

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