In React, a ref is an object with a single .current property that persists between renders. Unlike state, updating a ref does not trigger a re-render of your component. This makes them the primary tool for escaping the standard React data flow. You can point a ref to any value. However, the most common use case for a ref is to access a DOM element.
For example, this is handy if you want to focus an input programmatically. When you pass a ref to a ref attribute in JSX, like <div ref={myRef}>, React will put the corresponding DOM element into myRef.current. Once the element is removed from the DOM, React will update myRef.current to be null.
Managing focus, text selection, or media playback.
Triggering imperative animations.
Timer IDs, previous props or state
External Library Instances (Mapbox/Google Maps, echarts)
Integrating with third-party DOM libraries.
You need to focus an input element automatically when a modal opens. How would you use a ref to accomplish that?
If you attach a ref to a custom child component, what value do you receive and how could you call a method on that component?
What happens if you try to read a ref's .current property before the component has mounted?
Our team added a third‑party chart library that expects a DOM node, but after a refactor it throws ‘null is not an object’. Walk me through how you'd debug the ref usage.
We want to animate a list item's height using the Web Animations API. Explain how you'd store the element reference and why you wouldn't use state for this.
When converting a class component that used this.myRef = React.createRef() to a functional component, what changes are required and what pitfalls might appear?
In a large form we need to programmatically scroll to the first invalid input after validation. Describe a scalable approach using refs, considering performance and cleanup.
A component must expose an imperative handle (e.g., focus, reset) to its parent while staying reusable. How would you implement this with forwardRef and useImperativeHandle, and what trade‑offs does it introduce?
Discuss the implications of storing a mutable object like a canvas context in a ref versus in state when building a high‑frequency drawing app.
Our legacy codebase mixes string refs, callback refs, and createRef across many modules. Propose a migration strategy to unify ref usage, addressing backward compatibility and team onboarding.
We are building a design‑system library that must work with both React 16 (class components) and React 18 (hooks). How would you design the ref API to be consistent and future‑proof, and what constraints does this impose on the library's internal architecture?
When embedding a React tree inside a non‑React framework (e.g., Angular), what considerations around refs and DOM ownership arise, and how would you manage them to avoid memory leaks?