React Native - Use a keyExtractor with FlatList

前端 未结 8 1293
暖寄归人
暖寄归人 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:49

    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

     item.id} data={courseGoals} renderItem={itemData =>itemData.item.value } />
    

提交回复
热议问题