DEV Community

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

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.