Load a user when the component appears and again when the id changes, and ignore answers that arrive too late.
React · beginner · 9 min · 44 steps · free
import { useState, useEffect } from 'react';
// Stands in for a real request: answers after half a second.
function fetchUser(id) {
return new Promise((resolve) => {
setTimeout(() => resolve({ id, name: id === 1 ? 'Ada' : 'Linus' }), 500);
});
}
export default function UserCard() {
const [userId, setUserId] = useState(1);
const [user, setUser] = useState(null);
useEffect(() => {
let ignore = false;
fetchUser(userId).then((data) => {
if (!ignore) setUser(data);
});
return () => {
ignore = true;
};
}, [userId]);
return (
<div>
{user ? <h3>{user.name}</h3> : <p>Loading…</p>}
<button onClick={() => setUserId(userId === 1 ? 2 : 1)}>Next user</button>
</div>
);
}<UserCard> for the first time: it calls the UserCard function.useState runs for the first time, so React stores the starting value for userId and returns it: 1.useState runs for the first time, so React stores the starting value for user and returns it: null.useEffect does not run the effect now. React runs it after the page has been updated, and again only when a dependency changes.UserCard returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.ignore starts false. The cleanup will set it to true if this request becomes out of date.setTimeout(() => resolve({ id, name: id === 1 ? 'Ada' : 'L….fetchUser returns a Promise straight away. The answer comes later.fetchUser(userId).then((data) => { if (!ignore) setUser(da….ignore is still false, so setUser(data) stores the user.<UserCard> again (render 2): it calls UserCard once more, from the top.useState returns the value React has kept for userId: 1. The starting value is only used on the first render.useState returns the value React has kept for user: { id: 1, name: "Ada" }. The starting value is only used on the first render.useEffect does not run the effect now. React runs it after the page has been updated, and again only when a dependency changes.UserCard returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.onClick handler.setUserId(userId === 1 ? 2 : 1) does not change userId right away: React queues the update and will render again. In this call userId is still 1.<UserCard> again (render 3): it calls UserCard once more, from the top.useState returns the value React has kept for userId: 2. The starting value is only used on the first render.useState returns the value React has kept for user: { id: 1, name: "Ada" }. The starting value is only used on the first render.useEffect does not run the effect now. React runs it after the page has been updated, and again only when a dependency changes.UserCard returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.ignore is now true for the old request.ignore = false.setTimeout(() => resolve({ id, name: id === 1 ? 'Ada' : 'L….fetchUser returns Promise (pending).fetchUser(userId).then((data) => { if (!ignore) setUser(da….resolve({ id, name: id === 1 ? 'Ada' : 'Linus' }).!ignore is true, so the if-block runs.<UserCard> again (render 4): it calls UserCard once more, from the top.useState returns the value React has kept for userId: 2. The starting value is only used on the first render.useState returns the value React has kept for user: { id: 2, name: "Linus" }. The starting value is only used on the first render.useEffect does not run the effect now. React runs it after the page has been updated, and again only when a dependency changes.UserCard returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.Answer: So an old answer cannot overwrite newer data. The request still finishes; its result is ignored.
Answer: An async function returns a Promise, not a cleanup
← Effect Cleanup · useRef: Values That Don't Re-render →