DEV Community

Cover image for Using refs to check if a component is still mounted.

Using refs to check if a component is still mounted.

Tushar Kashyap on October 15, 2020

While reading Learning React by Alex Banks and Eve Porcello, I came across a very neat method to check if a component is still mounted or not. Thi...
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.

Collapse
 
soullivaneuh profile image
Sullivan SENECHAL • Edited

I am quite confused.

I did something like this (but with a local var instead), but I revert my change after finding this official article: reactjs.org/blog/2015/12/16/ismoun...

However, this article is talking about this.isMounted from a component class, not a hook.

My question is: Is it the same thing, and in this case also anti-pattern or what is the difference?

Thanks for the article!

Collapse
 
tusharkashyap63 profile image
Tushar Kashyap

See if you are able to cancel the request/promise in the clean up function of useEffect and check if it was cancelled before updating the state, that is basically the gist of the official React blog in the age of hooks.

"An optimal solution would be to find places where setState() might be called after a component has unmounted, and fix them."

This article does that exactly.

Any place you feel cancelling the process or request is not an option or too much work you can definitely use this, just a tool in your arsenal.

Collapse
 
borys_kupar profile image
Borys Kupar

Though this does a job, but it's still a workaround. I think the preferable way is to try to clean-up any side-effects when component is unmounted. In your example, setTimeout function actually returns timeout id, that you could use to do clearTimeout(timeoutId) on component unmount.

Collapse
 
tusharkashyap63 profile image
Tushar Kashyap • Edited

Yeah agreed, but the real deal is that we have a method to check if a component is unmounted, we can use it anywhere we feel the need to have this functionality.

Collapse
 
futureistaken profile image
R Z

Do you really think it's good to keep fetching data when the user has left the current page? The request should be aborted, not just suppressed.

Collapse
 
pallymore profile image
Yurui Zhang
Collapse
 
blackr1234 profile image
blackr1234 • Edited

This is a useful article too, thanks for the reference.

Collapse
 
tusharkashyap63 profile image
Tushar Kashyap • Edited

Here, the request is being made just one additional time and that too because a user got frustrated(this won't be happening for the majority of time)

Abort Controller isn't even supported by IE11 and I think a wasted API call isn't always as bad as a memory leak.

Collapse
 
karimkamel profile image
KarimKamel

what is the advantage of using "useRef.current" instead of just creating a variable "var isMounted = false(or true)"

Collapse
 
tusharkashyap63 profile image
Tushar Kashyap • Edited

I already answered this in a reply to another comment.

"I follow a general rule of using refs if I don't want to re-render, and using state if I want to re-render. I use local variables only for the derived state(incoming props). Also it was giving me a linting error as local variables value is lost on every re-render(which is fine in this case)."

Collapse
 
sankarrajendran profile image
Sankar-Rajendran

Why can't we use a plain variable like let isMounted = true and make it false at the unmount function( clean up). Useref is little overhead for this purpose right?

Collapse
 
tusharkashyap63 profile image
Tushar Kashyap

I follow a general rule of using refs if I don't want to re-render, and using state if I want to re-render. I use local variables only for the derived state(incoming props). Also it was giving me a linting error as local variables value is lost on every re-render(which is fine in this case).

Collapse
 
tusharkashyap63 profile image
Tushar Kashyap

This is why I love publishing to dev.to. It actually improves the article.