I am getting an error message of Warning: 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 useEffect cleanup function.
I have used the cleanUp in useState but still I am getting error.
Here is the code:
let isComponentMounted = useRef(false);
useEffect(()=>{
isComponentMounted.current = true;
setLoading(true);
if(isComponentMounted){
async function fetchData(){
const data = await fetch(`/data`)
const res = await data.json();
setLoading(false);
return res
}
fetchData().then((response)=>{
setMovie(response.data.movies[Math.floor(Math.random()*response.data.movies.length-1)])
})
}
return()=>{isComponentMounted.current = false; setMovie()}
},[])
I am new to react it would be really helpful