[React] key for map
when need use each elements in array,
react usually using map.
map is iterate the array and return each value in array.
map need key for identifier each elements.
and key is should be unique.
because for identity.
how make unique key?
there was several way.
one of them is using useRef().
useRef() has ‘current’ value.
therefore set the value to useRef() like 0 or 1,
and plus the number each iterate.
the other way is setting number and plus. like using useRef().
and there was the way of using index!
map has each index every iterate. and using that index.
but it is not recommended usually.
because index is subordinate array’s structure. that means index is possibly change by changed array’s structure.
easy and great thing is each elements has original and unique id!
then using that id!
but elements not has id, we can make id!
before I said, map can use index. and also can use value.
thus, combined them!
like,
const list = someArray.map((value, index) =>
<li key={`${index}_${value}`}>{value}</li>)
then, key has unique value!