Skip to content

HTML / CSS

React Refs and Callback Refs

React gives you two ways to grab a reference to a DOM element: object refs (via useRef) and callback refs. They both give you access to the underlying DOM...

24 Apr 2024 · 2 min read

React Refs and Callback Refs

React gives you two ways to grab a reference to a DOM element: object refs (via useRef) and callback refs. They both give you access to the underlying DOM node, but they behave differently in ways that matter.

Object refs with useRef

useRef creates a mutable object with a .current property. You attach it to a JSX element, and React populates .current with the DOM node after render.

Tsx
import React, { useRef, useEffect } from 'react'

const AutoFocusInput: React.FC = () => {
  const inputRef = useRef<HTMLInputElement>(null)

  useEffect(() => {
    inputRef.current?.focus()
  }, [])

  return <input ref={inputRef} type="text" placeholder="I focus on mount" />
}

This is the approach most developers reach for. It's simple. The ref object persists across renders without causing re-renders. You access the DOM node whenever you need it through .current.

Callback refs

A callback ref is a function. React calls it with the DOM node when the component mounts, and calls it with null when it unmounts.

Tsx
import React, { useCallback } from 'react'

const MeasuredBox: React.FC = () => {
  const measuredRef = useCallback((node: HTMLDivElement | null) => {
    if (node !== null) {
      const { height } = node.getBoundingClientRect()
      console.log(`Box height: ${height}px`)
    }
  }, [])

  return <div ref={measuredRef}>Measure me</div>
}

The key difference: callback refs fire at the moment the node attaches or detaches. You don't need a useEffect to react to the node being available. This makes them ideal for measuring elements or integrating with third-party libraries that need the actual DOM node at initialization time.

When to use which

Use useRef when you need to store a reference and access it later -- focusing an input on a button click, scrolling to an element, or reading a value imperatively.

Use callback refs when you need to do something the instant the node appears or disappears -- measuring dimensions, initializing a third-party library, or tracking element visibility.

The trade-off

Object refs are simpler and cover 90% of use cases. Callback refs are more powerful but add complexity. If you wrap a callback ref in useCallback, you need to manage its dependency array. If you don't, React creates a new function on every render, and the callback fires on every render too (with null, then the node again).

Pick useRef by default. Reach for callback refs when timing matters.

Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.

Go further
Live cohort on Maven

Forward Deployed AI Engineering: From Customer Problem to Production Value

A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.

January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.

View the live cohort

Keep reading