Skip to content

Instantly share code, notes, and snippets.

@Drag13
Created February 10, 2023 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drag13/0599ba7e14ceb2fc25847fce8d9974ac to your computer and use it in GitHub Desktop.
Save Drag13/0599ba7e14ceb2fc25847fce8d9974ac to your computer and use it in GitHub Desktop.
Example how to create key in list
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