简体   繁体   English

反应动态列表中的键

[英]React keys in dynamic lists

This is from the Facebook documentation: 这来自Facebook文档:

When a list is rerendered, React takes each element in the new version and looks for one with a matching key in the previous list. 重新提交列表后,React会使用新版本中的每个元素,并在上一个列表中查找具有匹配键的元素。 When a key is added to the set, a component is created; 将密钥添加到集合中时,将创建一个组件; when a key is removed, a component is destroyed. 移除键后,组件将被破坏。 Keys tell React about the identity of each component, so that it can maintain the state across rerenders. 密钥将每个组件的身份告知React,以便它可以在重新渲染时保持状态。 If you change the key of a component, it will be completely destroyed and recreated with a new state. 如果更改组件的键,它将被完全销毁并以新状态重新创建。

It's strongly recommended that you assign proper keys whenever you build dynamic lists. 强烈建议您在构建动态列表时分配适当的键。 If you don't have an appropriate key handy, you may want to consider restructuring your data so that you do. 如果没有合适的密钥,则可能需要考虑重组数据,以便这样做。

If you don't specify any key, React will warn you and fall back to using the array index as a key – which is not the correct choice if you ever reorder elements in the list or add/remove items anywhere but the bottom of the list. 如果您未指定任何键,React会警告您并退回使用数组索引作为键–如果您对列表中的元素重新排序或在底部以外的任何位置添加/删除项目,则这不是正确的选择。名单。 Explicitly passing key={i} silences the warning but has the same problem so isn't recommended in most cases. 显式传递key = {i}可使警告静音,但存在相同的问题,因此在大多数情况下不建议这样做。

Component keys don't need to be globally unique, only unique relative to the immediate siblings. 组件键不必是全局唯一的,而仅相对于直接同级是唯一的。

I don't get it. 我不明白 If we don't supply keys in a list like this: 如果我们不在这样的列表中提供密钥:

return (
        <li>
          <a href="#" onClick={() => this.jumpTo(move)}>{desc}</a>
        </li>
      );

What is the problem? 问题是什么? If the list had 3 elements and the keys were [0,1,2] and we removed 1 , what would happen? 如果列表包含3个元素,并且键为[0,1,2]而我们删除了1 ,那会发生什么?

Each list needs a key for only React to use; 每个列表都需要一个密钥,仅供React使用。 like this <li key={item.uniqueId}> where the uniqueId of the item is unique in the group of items. 这样<li key={item.uniqueId}>其中uniqueId所述的item是项目组中唯一的。 So when you modify this list React knows how to deal with the modification. 因此,当您修改此列表时,React知道如何处理修改。 This key is different from the index/positioning of a single item in a list. 此键与列表中单个项目的索引/位置不同。

When you create a list using a map like so you'll do something like this: 当您使用类似的地图创建列表时,您将执行以下操作:

return (
    <ul>
       {this.props.items.map(item => <ItemComponent key={item.id} {...item} />)}
    </ul>
)

In the documentation you posted above you could use the item's index (ie, (index, i) => <ItemComponent key={i} ) but it's not recommended. 在上面发布的文档中, 可以使用项目的索引(即(index, i) => <ItemComponent key={i} ),但不建议这样做。

In the ItemComponent list component: ItemComponent列表组件中:

return (
  <li>
    {/* your code here */}
  </li>
)

Or using your example you could do it like this: 或者使用您的示例,您可以这样做:

return (
  <li key={this.props.uniqueId}>
    <a href="#" onClick={() => this.jumpTo(move)}>{desc}</a>
  </li>
);

Any time you move, delete or add items to this list React will know how to render it. 每当您在列表中移动,删除或添加项目时,React都会知道如何渲染它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM