DEV Community

Discussion on: HTTP-API Integration with React Lifecycle-Methods and Hooks

Collapse
 
kylegalbraith profile image
Kyle Galbraith

I'm not sure if what I am asking fits into that question exactly. I guess I'm wondering if in the hooks world there is the explicit unmounting that the abort could be called in. It seems a bit odd that useEffect implicitly mimicks the unmount and would only then execute the callback function.

This is likely a knowledge gap in my React, but I was hoping you might be able to provide a clearer take on it.

Thread Thread
 
kayis profile image
K • Edited

Ah, okay.

You supply a callback to the useEffect hook and this callback can return another callback.

The first callback is called on a mount event, the returned callback is the one that is called on an unmount event.

(If you supply an array as second argument to useEffect, the first callback is also executed if one of the elements intl this array changed between renders, if the array is empty, the first callback is only called in mount, like with my example)