DEV Community

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

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).