Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<VirtualizedList> has duplicate key, but it shouldn't

I am using in the following way:

<VirtualizedList
  onEndReachedThreshold={DEFAULT_THRESHOLD_VALUE}
  getItem={(data, index) => data[index]}
  getItemCount={(data) => data.length}
  keyExtractor={(item, index) => item.id + index}
  {...props}
  data={listData}
  onEndReached={getNextPageData}
  ListFooterComponent={footerComponentJsx}
/>

As you can see, for keyExtractor prop, I'm using the index as well as item.id, so I don't think I should get any duplicate keys. Yet, I get this error:

 ERROR  Warning: Each child in a list should have a unique "key" prop.

Can someone give me some insights on why this is happening and how to remedy this?

like image 409
J.Ko Avatar asked Oct 17 '25 12:10

J.Ko


1 Answers

It is because there are maybe the same keys for an item in your:

 keyExtractor={(item, index) => item.id + index}

So be clear about item.id+index is not the same for two item.

So do one thing give a unique id for each item in item.id and then use it as below:

   keyExtractor={(item, index) => item.id}
like image 152
Jatin Bhuva Avatar answered Oct 20 '25 03:10

Jatin Bhuva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!