DEV Community

Discussion on: Using refs to check if a component is still mounted.

Collapse
 
ramblingenzyme profile image
Satvik Sharma

Not sure how I feel about this, but from my understanding, useEffect runs the cleanup function before each run of the effect, not just when the component updates. So, passing an empty array to the useEffect will make sure it's only set to false on unmount instead of flipping it between renders.

Collapse
 
blackr1234 profile image
blackr1234

I'm wondering why not an empty array too.

Collapse
 
soullivaneuh profile image
Sullivan SENECHAL

I did the same useMountedRef hook but with an empty array usage and it change not a thing. So yes, I would also prefer that way IMHO.