How unique should a React component Key be?

后端 未结 2 507
野趣味
野趣味 2020-12-18 19:06

This is a quick one: what is the scope in which the children keys have to be unique? Is this just the parent component or the whole app?

If the latter is true, does

2条回答
  •  半阙折子戏
    2020-12-18 19:45

    Unique within its siblings, so the level directly under its parent. The children of the parent component however can use the same key again, because finally, react will compose the complete key out of the key of the current component and all of its ancestors. The example below only contains auto-generated keys, but if you provide your own key, it will be used instead.

    You can see the composed ID's in Chrome developer tools (tab Elements).

提交回复
热议问题