Can't perform a React state update on an unmounted component

React JavaScript Library

Severity: Minor

What it means

React 17 and older, in development: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
For class components the end reads: in the componentWillUnmount method.
React 18 removed this warning. React's upgrade guide: people mostly ran into it where setting state is fine, and the workarounds made code worse.

Affected Models

  • React 16 and 17

How to Fix It

  1. On React 18 or newer, you won't see it. Upgrading removes the warning.

  2. Staying on React 17? Return a cleanup function from the effect that stops what it started: clearInterval for a timer, the unsubscribe call for a subscription.

  3. A fetch that may finish after the component is gone? React's docs use an ignore flag: let ignore = false at the start of the effect, return () => { ignore = true; }, and set state only if !ignore.

  4. Class components: cancel the same things in componentWillUnmount.