React Native - Use a keyExtractor with FlatList

前端 未结 8 1166
暖寄归人
暖寄归人 2021-02-01 14:05

I have been getting the:

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

相关标签:
8条回答
  • 2021-02-01 14:42

    Try using listkey instead of keyExtractor, it worked for me

    0 讨论(0)
  • 2021-02-01 14:46

    "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)}
    />
    
    0 讨论(0)
  • 2021-02-01 14:47

    Actually, in my case, FlatList data prop value was not an array type (it was empty object) so when I add keyExtractor={(item, index) => item.key} it made an error.

    I hope it helps you.

    0 讨论(0)
  • this will work sure. First generate random number.

    let generateRandomNum = () => Math.floor(Math.random() * 1001);
    

    then call where you need generate random number. like :-

    { id: generateRandomNum().toString(), value: 'your value'}
    

    //below i share small part of my code for better understand

    const addGoalHandler = enteredInputGoal => {
    console.log('random nub', generateRandomNum());
    setCourseGoals(courrentGoals => [...courrentGoals, { id: generateRandomNum().toString(), value: enteredInputGoal }]);
      };
    

    and finally use in FlatList

    <FlatList keyExtractor={(item, index) => item.id} data={courseGoals} renderItem={itemData =>itemData.item.value } />
    
    0 讨论(0)
  • 2021-02-01 14:51

    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();  };
    
    0 讨论(0)
  • 2021-02-01 15:02

    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.

    0 讨论(0)
提交回复
热议问题