Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose react key for a list with duplicate items

Tags:

reactjs

I need to render a list with string items like ['a', 'b', 'c', 'a'], the items are allowed to be duplicate to each other, what is the proper way to choose a react key in {list.map(x => <span key={?}>{x}</span>)}?

like image 544
golopot Avatar asked Dec 16 '25 12:12

golopot


1 Answers

This should work

{list.map((x,i) => <span key={`${x}-${i}`}>{x}</span>)}
like image 123
normalcepalin Avatar answered Dec 19 '25 05:12

normalcepalin



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!