For example, if we have an array
0 => San Francisco
1 => Taipei
2 => San Jose
and use the index as the "key" for every <tr>
element like this
this.props.arrData.map((data, i) => {
return (
<tr key={i}>
// ...
it should be fine. When we add to the end of array:
3 => Sacramento
And the index to data mapping is still all good -- just one more mapping is added.
However, I found that even if it is prepended to the array, and still use the index as the "key", it still work perfectly and don't know why.
I thought it should not work because at the very beginning with 1 record:
0 => San Francisco
and it will cache the key 0 as the HTML fragment of San Francisco.
And when a record is prepended:
0 => Taipei
1 => San Francisco
I thought React actually use the 0
as the key to this row, and somehow can "cache" this fragment, and so any <tr>
with key=0, it would have it cached as San Francisco, so the table could become:
0 => San Francisco
1 => San Francisco
But it doesn't. So if something was cached, why would it actually work?
P.S. I ended up using:
var countTotalItems = this.props.weatherList.length;
return this.props.weatherList.map((weather, i) => {
return (
<tr key={`${countTotalItems - i} ${weather.cityName} ${weather.countryCode}`}>
// ...
and the key
is like: 1 San Francisco US
for the last row all the time, even when there are more items added to the array -- essential, just reverse the order by using a key of 1
at the end of array. I used the city and country as well to better identify the row, just in case, for example, if in the future a row can be deleted by the user. But the question is, why if not reversing the order, it still works.
P.P.S. as I put in the comment of one answer: if the row is all text, I found no difference in using key={cityName + i}
vs key={i}
, but if the row contains an image using <img />
of the weather condition, I do find a difference. If it is key={i}
then the newest row will have the previous image for 1 to 2 seconds, and then the correct image will get in. But if it is key={cityName + i}
, then the image will be blank, and then 1 to 2 seconds later, the image will get in. The latter behavior is more desirable... the former behavior can be a little confusing to the user.