You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/lists-and-keys.md
+43-43Lines changed: 43 additions & 43 deletions
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,30 @@
1
1
---
2
2
id: lists-and-keys
3
-
title: Lists and Keys
3
+
title: 列表 & Keys
4
4
permalink: docs/lists-and-keys.html
5
5
prev: conditional-rendering.html
6
6
next: forms.html
7
7
---
8
8
9
-
First, let's review how you transform lists in JavaScript.
9
+
首先,让我们看下在 Javascript 中如何转化列表。
10
10
11
-
Given the code below, we use the [`map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)function to take an array of `numbers` and double their values. We assign the new array returned by `map()` to the variable `doubled` and log it:
Below, we loop through the `numbers` array using the JavaScript [`map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)function. We return a `<li>`element for each item. Finally, we assign the resulting array of elements to `listItems`:
We include the entire `listItems`array inside a `<ul>`element, and [render it to the DOM](/docs/rendering-elements.html#rendering-an-element-into-the-dom):
When you run this code, you'll be given a warning that a key should be provided for list items. A "key" is a special string attribute you need to include when creating lists of elements. We'll discuss why it's important in the next section.
73
+
当我们运行这段代码,将会看到一个警告 `a key should be provided for list items` ,意思是当你创建一个元素时,必须包括一个特殊的 `key` 属性。我们将在下一节讨论这是为什么。
74
74
75
-
Let's assign a `key`to our list items inside `numbers.map()` and fix the missing key issue.
75
+
让我们来给每个列表元素分配一个 `key`属性来解决上面的那个警告:
76
76
77
77
```javascript{4}
78
78
function NumberList(props) {
@@ -94,11 +94,11 @@ ReactDOM.render(
94
94
);
95
95
```
96
96
97
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/jrXYRR?editors=0011)
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity:
The best way to pick a key is to use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys:
112
+
一个元素的 key 最好是这个元素在列表中拥有的一个独一无二的字符串。通常,我们使用来自数据 id 来作为元素的 key:
We don't recommend using indexes for keys if the order of items may change. This can negatively impact performance and may cause issues with component state. Check out Robin Pokorny's article for an [in-depth explanation on the negative impacts of using an index as a key](https://medium.com/@robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318). If you choose not to assign an explicit key to list items then React will default to using indexes as keys.
133
+
如果列表项目的顺序可能会变化,我们不建议使用索引来用作键值,因为这样做会导致性能变差,还可能引起组件状态的问题。可以看看 Robin Pokorny 的[深度解析使用索引作为 key 的负面影响](https://medium.com/@robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318) 这一篇文章。如果你选择不指定显式的键值,那么 React 将默认使用索引用作为列表项目的键值。
134
134
135
-
Here is an [in-depth explanation about why keys are necessary](/docs/reconciliation.html#recursing-on-children) if you're interested in learning more.
Keys only make sense in the context of the surrounding array.
139
+
元素的 Key 只有放在就近的数组上下文中才有意义。
140
140
141
-
For example, if you [extract](/docs/components-and-props.html#extracting-components)a`ListItem`component, you should keep the key on the `<ListItem />`elements in the array rather than on the `<li>`element in the `ListItem` itself.
Keys used within arrays should be unique among their siblings. However they don't need to be globally unique. We can use the same keys when we produce two different arrays:
Keys serve as a hint to React but they don't get passed to your components. If you need the same value in your component, pass it explicitly as a prop with a different name:
Sometimes this results in clearer code, but this style can also be abused. Like in JavaScript, it is up to you to decide whether it is worth extracting a variable for readability. Keep in mind that if the `map()`body is too nested, it might be a good time to [extract a component](/docs/components-and-props.html#extracting-components).
0 commit comments