We use refs when you want a component to “remember” some information, but don’t want that information to trigger new renders.
ref returns a reference to the element.
Refs are an escape hatch to hold onto values that aren’t used for rendering. You won’t need them often.
A ref is a plain JavaScript object with a single property called current, which you can read or set.
You can ask React to give you a ref by calling the useRef Hook.
Like state, refs let you retain information between re-renders of a component.
Unlike state, setting the ref’s current value does not trigger a re-render.
Don’t read or write ref.current during rendering. This makes your component hard to predict.
If your component needs to store some value, but it doesn’t impact the rendering logic, choose refs.
Storing timeout IDs
Storing and manipulating DOM elements
Storing other objects that aren’t necessary to calculate the JSX.
You need to focus an input field automatically when a modal opens. How would you use a ref to accomplish that?
If you try to read a DOM element’s size directly in a functional component without a ref, what issues might you run into?
How would you store a mutable counter that persists across renders without causing the component to re‑render?
We’re adding a third‑party chart library that requires a container DOM node. Walk me through setting it up with refs and any pitfalls to watch for.
A component that uses a ref to measure its width stops updating after a state change. What could be causing that bug?
Why might you choose a ref instead of state to track whether a dropdown is open, and what trade‑offs does that involve?
In a virtualized list we need to keep the scroll position without triggering re‑renders. How would you design that using refs?
Explain how forwardRef affects component composition and what pitfalls you need to guard against in a shared component library.
We have a performance‑critical animation that updates DOM properties on every frame. How would you use refs to avoid React’s reconciliation overhead?
Our legacy codebase uses string refs and findDOMNode extensively. What migration strategy would you propose to replace them with modern ref patterns while minimizing risk?
When building a UI framework used across multiple products, how do you decide which internal state belongs in refs versus React state to balance performance and debuggability?
Describe the guidelines you’d set for cross‑team usage of forwardRef and useImperativeHandle to expose imperative APIs without breaking encapsulation.