Skip to content

Instantly share code, notes, and snippets.

@Drag13
Created February 1, 2024 21:05
Show Gist options
  • Save Drag13/397482e782ed401d09b7d2ce14c126fa to your computer and use it in GitHub Desktop.
Save Drag13/397482e782ed401d09b7d2ce14c126fa to your computer and use it in GitHub Desktop.
// imagine this function is out of the file
function fetchUser(id: number, signal: AbortSignal) {
return fetch(`/user/${id}`, { signal }).then(x=>x.json());
}
function UserDetails({ userId }: { userId: number }) {
const [user, setUser] = useState(null);
useEffect(() => {
const abortController = new AbortController();
fetchUser(userId, abortController.signal).then(setUser);
return () => abortController.abort();
}, [userId]);
return <>{user?.userId}</>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment