React useEffect causing: Can't perform a React state update on an unmounted component React useEffect causing: Can't perform a React state update on an unmounted component reactjs reactjs

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


For me, clean the state in the unmount of the component helped.

 const [state, setState] = useState({});useEffect(() => {    myFunction();    return () => {      setState({}); // This worked for me    };}, []);const myFunction = () => {    setState({        name: 'Jhon',        surname: 'Doe',    })}


Sharing the AbortController between the fetch() requests is the right approach.
When any of the Promises are aborted, Promise.all() will reject with AbortError:

function Component(props) {  const [fetched, setFetched] = React.useState(false);  React.useEffect(() => {    const ac = new AbortController();    Promise.all([      fetch('http://placekitten.com/1000/1000', {signal: ac.signal}),      fetch('http://placekitten.com/2000/2000', {signal: ac.signal})    ]).then(() => setFetched(true))      .catch(ex => console.error(ex));    return () => ac.abort(); // Abort both fetches on unmount  }, []);  return fetched;}const main = document.querySelector('main');ReactDOM.render(React.createElement(Component), main);setTimeout(() => ReactDOM.unmountComponentAtNode(main), 1); // Unmount after 1ms
<script src="//cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.development.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.development.js"></script><main></main>