Created
February 1, 2024 21:05
-
-
Save Drag13/397482e782ed401d09b7d2ce14c126fa to your computer and use it in GitHub Desktop.
This file contains 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
// 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