What is the useEffect cleanup function?
It is a function of the useEffect hook that allows us to stop side effects that no longer need to be executed before our component is unmounted. For example, Component A requests the API to get a list of products, but while making that asynchronous request, Component A is removed from the DOM (it’s unmounted). There is no need to complete that asynchronous request.
What is the solution??
Cleanup function:
Canceling a fetch request
There are different ways to cancel fetch request calls, we can use fetch AbortControlleror Axios AbortController.
This associates the controller and signal with the fetch request and lets us cancel it anytime using AbortController.abort():