Created
February 10, 2023 07:47
-
-
Save Drag13/0599ba7e14ceb2fc25847fce8d9974ac to your computer and use it in GitHub Desktop.
Example how to create key in list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { v4 as uuidv4 } from "uuid"; | |
| import { useEffect, useState } from "react"; | |
| // Geting user from the "server" | |
| function fetchUsers() { | |
| return fetch("/users") | |
| .then((x) => x.json()) | |
| .then((users) => { | |
| return users.map((u) => { | |
| // creating the key with uuid package | |
| u.userKey = uuidv4(); | |
| return u; | |
| }); | |
| }); | |
| } | |
| function AppList() { | |
| const [users, setUsers] = useState([]); | |
| useEffect(() => { | |
| fetchUsers().then(setUsers); | |
| }, []); | |
| return ( | |
| <ul> | |
| {users.map(({ userKey, name }) => ( | |
| <li key={userKey}>{name}</li> | |
| ))} | |
| </ul> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment