Skip to content

Ref

In one line

A ref is an object with a .current property that holds a reference to a DOM element or a mutable value that persists across renders.

In simple words

A ref (reference) is an escape hatch from React's declarative model. It gives you direct access to a DOM element or a mutable value that doesn't trigger re-renders.

You create a ref with useRef(initialValue) and attach it to a DOM element via the ref attribute: <input ref={myRef} />. After mount, myRef.current points to the actual DOM node, and you can call imperative methods like .focus(), .scrollIntoView(), or .value.

Refs are also used to store any mutable value that should persist across renders but shouldn't cause a re-render when changed — a timer ID, a previous prop value, or a "has mounted" flag. Think of refs as instance variables for function components.

Example

jsx
const timerRef = useRef(null);

timerRef.current = setInterval(() => {
  console.log("tick");
}, 1000);

// Later: clearInterval(timerRef.current);

The ref stores a timer ID that persists across renders. Changing .current doesn't trigger a re-render.

Related terms

Related Resources

Related Lessons

  • useRef

    Learn refs for DOM access and mutable storage.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary