Why doesn't React recommend using the index of the array as the key value?

A Nuggets article has a gif display, which is the easiest explanation I have seen.

https://juejin.im/post/5e9d83436fb9a03c917fe728

Believe it and understand it after reading it

Summary: When the index of the array is used as the key value, a change in one element may cause the key value of all elements to change. The diff algorithm compares the differences between the same levels and associates them with the key value. When performing subscript transformation on an array, such as deleting the first piece of data, then all indexes will change in the future, so the key value will naturally also change all, so index as a key value is unstable, this kind of instability It may lead to a waste of performance, resulting in diff not being able to associate the same data last time. Therefore, if you can't use Index as a key, don't use Index. 

In the usual development process, because most of our data is obtained from the background. Each piece of data in the database will have an id as a unique identifier, and this id is also the source of the key value we most often use.

Guess you like

Origin blog.csdn.net/a1059526327/article/details/106819237