I have the following piece of code on which I am getting warning two warnings:
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.
unmountComponentAtNode(): The node you're attempting to unmount was rendered by another copy of React.
import React, { useState, useEffect } from "react";
import GravatarList from "./GravatarList";
import Header from "./Header";
import { calculateNumberOfImages } from "./utils";
const App = () => {
const handle = () => {
setState({ images: calculateNumberOfImages() });
}
useEffect(() => {
return () => {
window.removeEventListener('resize', () => handle())
window.removeEventListener('scroll', () => handle())
}
}, [])
const [state, setState] = useState(() => {
window.addEventListener("scroll", () => handle());
window.addEventListener("resize", () => handle());
return { images: calculateNumberOfImages() };
});
return (
<div>
<Header />
<GravatarList state={state} />
</div>
);
};
export default App;
I can't seem to figure out what I am doing wrong as I have already cleared the listeners in useEffect.