Can't Perform a React State Update on an Unmounted Component

React Warning “Can't perform a react state update on an unmounted component” is caused when we try to update the state after the component was unmounted. 

To solve the issue we can set a flag to determine whether the component was unmounted or not.

Below is the sample code.

import {useState, useEffect} from 'react';

const App = () => {
  	const [state, setState] = useState('');

  	useEffect(() => {
    	// set isMounted to true
    	let isMounted = true;
	
    	someAsyncOperation().then(data => {
    		if (isMounted) setState(data);    // add conditional check
  		});
		
    	return () => {
      		// when component unmounts, set isMounted to false
      		isMounted = false;
    	};
  	}, []);
	
  	return (
    	<div>
      		<h2>State: {JSON.stringify(state)}</h2>
    	</div>
  	);
};

export default App;

Here we have initialized isMounted flag in useEffect and set it to true. And, this flag was checked before we updating the state after async operation as given below. 

let isMounted = true;
someAsyncOperation().then(data => {
    if (isMounted) setState(data);    // add conditional check
});

The function returned from the useEffect hook will be executed when the component unmounts. So we set isMounted variable into false in useEffect return function as given below.

return () => {
      	// when component unmounts, set isMounted to false
      	isMounted = false;
  	};

Learn using the Effect Hook and cleanup in react.

Related Blogs

Each child in a list should have a unique key prop

Each child in a list should have a unique key prop

Solve Warning: Each child in a list should have a unique ”key" prop in React by setting the id property as a unique key or by auto-assigning unique keys.

React Hook is Called Conditionally

React Hook is Called Conditionally

Error: "React Hook is called conditionally. React Hooks must be called in the exact same order in every component render" occurs when hooks are invoked conditionally or after a return of a value.

Too many re-renders. React limits the number of renders to prevent an infinite loop

Too many re-renders. React limits the number of renders to prevent an infinite loop

The React error "Too many re-renders. React limits the number of renders to prevent an infinite loop" happens when you have reached an infinite render loop.

Adjacent JSX elements must be wrapped in an enclosing tag

Adjacent JSX elements must be wrapped in an enclosing tag

The "Adjacent JSX elements must be wrapped in an enclosing tag" error can be solved by wrapping the multiple elements in a parent div or in a react fragment

The tag is unrecognized in this browser

Warning: The tag is unrecognized in this browser

Warning "The tag is unrecognized in this browser" occurs when the tag doesn't exist in the browser or when we render a component starting with a lowercase letter.

The style prop expects a mapping from style properties to values, not a string

The style prop expects a mapping from style properties to values, not a string

"The style prop expects a mapping from style properties to values, not a string" React error occurs when we pass a CSS string to the style attribute.

Pass event and parameter onClick in React

How to pass event and parameter onClick in React

We set the inline arrow function (event) => clickHandler(event, "Hello World!") as props to the onClick event handler in the button. The arrow function takes the event as an argument and calls the clickHandler function.

Create a Back button with React Router

Create a Back button with React Router

To create a back button with React Router use useNavigate() hook. Call navigate function eg. navigate(-1); inside the onClick function of the back button.

Call multiple functions onClick in React

Call multiple functions onClick in React

To call multiple functions onClick in React, pass an event handler function into the onClick prop of the element that can call as many other functions as necessary.