๐—ฆ๐˜๐—ผ๐—ฝ ๐— ๐—ฒ๐—บ๐—ผ๐—ฟ๐˜† ๐—Ÿ๐—ฒ๐—ฎ๐—ธ๐˜€ ๐—œ๐—ป ๐—ฅ๐—ฒ๐—ฎ๐—ฐ๐˜

You see this warning in your console: "Warning: Can't perform a React state update on an unmounted component."

This happens when an async task runs inside useEffect. If your component unmounts before the task finishes, React tries to update state on a component that is gone. This creates a memory leak.

The Problem

When you fetch data, a race condition occurs. The data arrives, but the component no longer exists.

Example of the error: useEffect(() => { fetch('/api/data') .then(res => res.json()) .then(data => { setData(data); }); }, []);

The Fix

Use a cleanup function. You have two main ways to solve this.

  1. Use a boolean flag Set a variable to track if the component is still active.

useEffect(() => { let isMounted = true;

fetch('/api/data') .then(res => res.json()) .then(data => { if (isMounted) { setData(data); } });

return () => { isMounted = false; }; }, []);

  1. Use AbortController This is better because it stops the network request entirely.

useEffect(() => { const controller = new AbortController();

fetch('/api/data', { signal: controller.signal }) .then(res => res.json()) .then(data => setData(data)) .catch(err => { if (err.name === 'AbortError') return; console.error(err); });

return () => { controller.abort(); }; }, []);

Best Practices

Why this matters

Proper cleanup does more than hide warnings. It prevents unnecessary re-renders. It stops bugs in production. It saves network resources.

Always plan for what happens when a component disappears.

Source: https://dev.to/bhargavigajula/stop-memory-leaks-in-their-tracks-react-useeffect-cleanup-explained-47a5