Can't perform a React state update on an unmounted component - TwoGears/hakomo-guides GitHub Wiki
If you try to change the state of a component, but the component has already been unmounted, this will throw error due to memory leak.
to avoid that, keep a variable that tracks mounted state and update the state only if it is mounted:
export default function Comp(props){
let mounted = useRef(true);
useEffect(()=>{
// If your effect returns a function, React will run it when it is time to clean up
return ()=>{
mounted.current = false;
}
,[]}
async function handleClick(){
if(mounted.current){
// set state.
}
}
}