Skip to content

Commit c0d08b8

Browse files
committed
Feat: Replace mentions of uuid with crypto.randomUUID
1 parent a8a4901 commit c0d08b8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

react/getting_started_with_react/keys_in_react.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ Keys are passed into the component or a DOM element as a prop. You should alread
4242
<div key={keyValue} />
4343
```
4444

45-
<span id="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+
<span id="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>
4646

4747
```jsx
4848
// a list of todos, each todo object has a task and an id
4949
const todos = [
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() },
5353
];
5454

5555
function TodoList() {
@@ -79,21 +79,21 @@ function MonthList() {
7979
}
8080
```
8181

82-
<span id="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+
<span id="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>
8383

8484
```jsx
8585
const todos = [
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() },
8989
];
9090

9191
function TodoList() {
9292
return (
9393
<ul>
9494
{todos.map((todo) => (
9595
// DON'T do the following i.e. generating keys during render
96-
<li key={uuid()}>{todo.task}</li>
96+
<li key={crypto.randomUUID()}>{todo.task}</li>
9797
))}
9898
</ul>
9999
);

0 commit comments

Comments
 (0)