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: react/getting_started_with_react/keys_in_react.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,14 +42,14 @@ Keys are passed into the component or a DOM element as a prop. You should alread
42
42
<div key={keyValue} />
43
43
```
44
44
45
-
<spanid="keys-from-data">Now that we know the syntax, the next question is: what should be used as a key? Ideally, they should be some identifier that is unique to each item in the list. Most databases assign a unique id to each entry, so you shouldn't have to worry about assigning an id yourself. If you are defining data yourself, it is good practice to assign a unique `id` to each item. You may use the [uuid package](https://www.npmjs.com/package/uuid) to generate a unique id. Let's look at an example:</span>
45
+
<spanid="keys-from-data">Now that we know the syntax, the next question is: what should be used as a key? Ideally, they should be some identifier that is unique to each item in the list. Most databases assign a unique id to each entry, so you shouldn't have to worry about assigning an id yourself. If you are defining data yourself, it is good practice to assign a unique `id` to each item. You can use the [`crypto.randomUUID()` function](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID) to generate a unique id. Let's look at an example:</span>
46
46
47
47
```jsx
48
48
// a list of todos, each todo object has a task and an id
49
49
consttodos= [
50
-
{ task:"mow the yard", id:uuid() },
51
-
{ task:"Work on Odin Projects", id:uuid() },
52
-
{ task:"feed the cat", id:uuid() },
50
+
{ task:"mow the yard", id:crypto.randomUUID() },
51
+
{ task:"Work on Odin Projects", id:crypto.randomUUID() },
52
+
{ task:"feed the cat", id:crypto.randomUUID() },
53
53
];
54
54
55
55
functionTodoList() {
@@ -79,21 +79,21 @@ function MonthList() {
79
79
}
80
80
```
81
81
82
-
<spanid="anti-pattern">Keys are straightforward to use, though there is an anti-pattern you should be aware of. Keys should never be generated on the fly. Using `key={Math.random()}` or `key={uuid()}`*while* rendering the list defeats the purpose of the key, as now a new `key` will get created for every render of the list. As shown in the above example, `key` should be inferred from the data itself.</span>
82
+
<spanid="anti-pattern">Keys are straightforward to use, though there is an anti-pattern you should be aware of. Keys should never be generated on the fly. Using `key={Math.random()}` or `key={crypto.randomUUID()}`*while* rendering the list defeats the purpose of the key, as now a new `key` will get created for every render of the list. As shown in the above example, `key` should be inferred from the data itself.</span>
83
83
84
84
```jsx
85
85
consttodos= [
86
-
{ task:"mow the yard", id:uuid() },
87
-
{ task:"Work on Odin Projects", id:uuid() },
88
-
{ task:"feed the cat", id:uuid() },
86
+
{ task:"mow the yard", id:crypto.randomUUID() },
87
+
{ task:"Work on Odin Projects", id:crypto.randomUUID() },
88
+
{ task:"feed the cat", id:crypto.randomUUID() },
89
89
];
90
90
91
91
functionTodoList() {
92
92
return (
93
93
<ul>
94
94
{todos.map((todo) => (
95
95
// DON'T do the following i.e. generating keys during render
0 commit comments