1function UserProfile({ userId }) {
2 const [user, setUser] = useState(null);
3
4 useEffect(() => {
5 fetch(`/api/users/${userId}`)
6 .then(r => r.json())
7 .then(data => setUser(data)); // may run after unmount
8 }, [userId]);
9
10 if (!user) return <Spinner />;
11 return <div>{user.name}</div>;
12}
no lines flagged
#056PracticeMedium18 min · 120 XP
State Update on Unmounted Component
An async fetch inside useEffect tries to update state after the component unmounts, causing a React warning and potential memory leak.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.