Tsallaka zuwa abun ciki
Justice Tech School

Fetching in an Effect

Load data in an effect and guard against stale responses.

Example
useEffect(() => {
  let ignore = false;
  fetch("/api/users/" + id)
    .then((r) => r.json())
    .then((d) => { if (!ignore) setUser(d); });
  return () => { ignore = true; };
}, [id]);

Exercise

Stale guard.

let  = false;

Quiz Test

Why guard with a flag?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.