I've been reading on why useRef
is useful (e.g. in this SO answer and in the articles it links to) and it does make sense to me. However I notice that in my code I've "simply" solved the issue of how to store state in a functional component in a way that does not trigger re-renders by keeping the state as a global-scoped variable declared in the same file as the functional component.
I realize this isn't appropriate if the same component is rendered at the same time in multiple places on the DOM, as useRef
supplies different state to different simultaneously rendered components whereas a file-scoped variable would be shared.
Is my mental model and assumptions correct and are there any other use cases or distinct advantages of useRef
versus a file-scoped variable?