React useRef Hook
The useRef Hook lets you reference a value that’s not needed for rendering. It is essentially an "escape hatch" out of React's typical unidirectional data flow.
Accessing the DOM
React relies on declarative UI logic—meaning you describe *what* you want the DOM to look like based on state. However, sometimes you need to imperatively command a DOM element to do something, like focusing an input, scrolling to a specific div, or measuring a node's dimensions. By passing a ref to a JSX element like <input ref={myRef} />, React assigns that DOM node to myRef.current.
Mutable Values & Re-renders
Whenever you update a state variable using useState, React re-renders your component. What if you need to keep track of a timer ID from setInterval, or how many times a button was clicked, without forcing the whole UI to re-draw?useRef creates a plain JavaScript object { current: initialValue } that persists for the full lifetime of the component. Modifying .current is completely synchronous and invisible to React's rendering system.
View Deep Dive Notes+
Rule of thumb: Do NOT read or write ref.current during the main render phase. If a value needs to be displayed on screen, it should be State, not a Ref. Read/write to refs only in event handlers (like onClick) or Effects (like useEffect).
