← Back to React, page 1

react-0019 · Medium mid

What are refs and the useRef hook, and how do you pass a ref to a child component in React 19?

A ref is a box that holds a value React does not track for rendering. useRef returns an object with a current property that persists across renders, and changing it does not cause a re-render. It is used to reach DOM nodes (focus, scroll, measure) and to keep mutable values such as timer ids. In React 19, a function component receives ref as an ordinary prop, so forwardRef is no longer needed.

Full answer

State is for values that appear on screen. When something must persist across renders but should not trigger one, use a ref. const ref = useRef(initial) returns the same object { current: initial } on every render.

There are two uses. The first is a DOM handle. Put ref={inputRef} on an element and React sets inputRef.current to the node after it mounts, so you can call inputRef.current.focus() or measure it. The second is a stored value that is not part of the UI, such as a timeout id, a previous value or a flag that an effect reads.

The rules are: do not read or write ref.current during rendering (except for lazy first-time setup), because that makes the component impure. Read and write it in event handlers and effects. Changing it never re-renders. If the UI must update, use state.

Passing a ref to your own component was awkward in older React: a function component could not receive ref, so you wrapped it in forwardRef. In React 19, ref is a normal prop for function components, so function FancyInput({ ref, ...props }) just works. forwardRef still runs for now and is expected to be deprecated, so new code does not need it. Classes still receive the ref as the component instance.

React 19 also lets a ref callback return a cleanup function. It runs when the element is removed or the callback changes, replacing the old pattern of calling the callback again with null.

For exposing only chosen methods to a parent, such as focus and scrollToEnd, add useImperativeHandle instead of handing out the whole node. Use refs sparingly. Data flow should go through props and state.

jsx
import { useState } from 'react';

export default function Measured() {
  const [width, setWidth] = useState(0);
  return (
    <div
      ref={(node) => {
        const observer = new ResizeObserver(([entry]) => setWidth(entry.contentRect.width));
        observer.observe(node);
        return () => observer.disconnect(); // cleanup, new in React 19
      }}
    >
      Width: {width}
    </div>
  );
}
jsx
import { useRef, useState } from 'react';

function FancyInput({ ref, ...props }) {
  return <input ref={ref} className="fancy" {...props} />;
}

export default function App() {
  const inputRef = useRef(null);
  const clicks = useRef(0);
  const [shown, setShown] = useState(0);

  return (
    <div>
      <FancyInput ref={inputRef} id="name-field" />
      <button onClick={() => inputRef.current.focus()}>Focus the field</button>
      <button
        onClick={() => {
          clicks.current += 1;
          setShown(clicks.current);
        }}
      >
        Count a click
      </button>
      <p>Clicks: {shown}</p>
    </div>
  );
}

FancyInput receives ref as a normal prop and hands it to the input, so the parent can focus it. The click counter lives in a ref and is copied to state only when it should appear on screen. Output of the run: active element id=name-field

Key points

  • useRef returns a stable object with a mutable current property. Changing it does not re-render.
  • Use refs for DOM access and for non-visual values such as timer ids.
  • Do not read or write ref.current during render.
  • React 19 passes ref to function components as a prop, so forwardRef is unnecessary.
  • Ref callbacks can return a cleanup function in React 19.

Common mistakes

  • Reading or writing ref.current during rendering.
  • Using a ref for data that should be shown on screen and expecting the UI to update.
  • Wrapping every component in forwardRef in React 19 code.
  • Using refs to imperatively change things React already manages, such as text content.
  • Forgetting that ref.current is null before the element mounts.

Follow-ups

  • When is useImperativeHandle appropriate?
  • What changed with ref callbacks and cleanup in React 19?
  • Why is a ref a poor way to share data between components?

Tips

  • Give both uses (DOM node and persistent value) in the first minute.
  • State the render-time rule. It shows you understand purity.
  • Mention the React 19 ref prop change to show current knowledge.