I have following code written in react and I am get memory leak error. I tried few hacks like calling abort and setting the state to null but nothing worked for me so far.
Error I am getting:
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 use Effect cleanup function.
My code snippet:
useEffect(() => {
//some const defined
fetch(myData)
.then(response => response.json())
.then(data => {
//some code
if (consition) {
//setting state
setData(abc);
setDataPresent(true);
}
});
// fix for memory leak error
return () => {
setData({});
setDataPresent(false);
};
}, [dep]);