When setState is called after unmounting the Component in an asynchronous clickHandler causes warning in console:
Warning: Can't perform a React state update on an unmounted component.
function App() {
const [show, setShow] = useState(true)
return {show && <Button
text="Click to remove"
clickHandler={() => setShow(false)}
/> }
}
function Button({ text, clickHandler }) {
const [state, setState] = useState(text);
const handleClick = async () => {
await clickHandler();
setState("I was clicked");
};
return <button onClick={handleClick}> {state}</button>;
}
Clicking the click to remove button will unmount the button and then update the state.
The goal is to set the state of the Button Component after the asynchronous call. This asynchronous call can but does not have to trigger the Button being unmounted. I am looking for a way to opt out of the setState after the asynchronous call.
I do not see how this can be avoided in a nice way using useEffect.
There are two possible workarounds in the example.
One uses useRef to check if the component is unmounted (like recommended in #14369 (comment) ). This does not feel very react-like.
The other workaround uses the recommended useEffect guard variable (like #14369 (comment)). But to get the clickHandler out of useEffect the clickHandler is stored in a state. However, to get a function into a state it needs to be wrapped inside another function, as the setState function of useState will call a function given as an argument.