Why doesn't react automatically generate key? for lists?

Why doesn"t react automatically generate key, for lists instead of developers writing their own key?

The uniqueness of

react generated by itself can be guaranteed, is it because the stability of key cannot be guaranteed?


because the stability of key cannot be guaranteed
because the content can be changed, but id cannot be changed, otherwise the rendering efficiency will be reduced


because the criterion of "uniqueness" is in the hands of the user, React cannot automatically judge it. For example, I can use age to generate a list for key, or I can distinguish it by sex. Where can React automatically get such information?

it would be very expensive to calculate key based on the Props of each element.

if you randomly generate an independent key each time, it is equivalent to no key, and it takes more time to generate key.


  1. react doesn't know which one is the only one? Did you say index ? What if I delete or insert an element in the middle?
  2. you might say that using a forever unique key, such as guid , severely degrades performance, causing react's reconciliation algorithm to be useless at all. So will react use guid by default? It's impossible.

1. React already generates an automatic key-like id on the node

this article is an old version: https://codeshelper.com/a/11.

look at the plug-in content of react in chrome and you can see that it has its own internal way.

2. Not every node requires developers to add key

to put it simply: child elements (or components) in the array-Children in an array

key is designed for developers. It is an auxiliary react to make more stable add, change, delete operations.

reference: https://reactjs.org/docs/list.

3. The new version already has the function of automatically adding key-React v16.2.0

key is already very troublesome. I think I need to customize what can be key and what can't be done in the future unless it is necessary. The Fragment component in v16.2.0 can automatically add key.

2017.9 has been announced in advance:

Starting with React 16.2.0, we are adding support for a special
fragment syntax to JSX that doesn't require keys.

reference: https://reactjs.org/blog/2017.

Menu