Skip to content
HooksChapter 17 / 37Intermediate4 min read

useRef

Learn the React useRef hook to reach DOM elements, remember values without re-rendering, and pass ref as a normal prop in React 19.

On this page

1. What is useRef?

useRef has two main jobs:

  1. Direct access to a DOM element, like focusing an input.
  2. Remembering a value that is not shown on screen, like a timer id.

2. Why does it matter?

Sometimes you need to remember a value without updating the UI. State re-renders on every change, which is wasted work here.

useStateuseRef
Changing it causes a re-renderChanging it causes no re-render
The value changes through the setterAssign to ref.current directly
Data that shows in the UIBehind-the-scenes data or a DOM node
Safe to read during renderDo not read or write it during render

3. Where is it used?

Use caseWhat the ref holds
Input focusThe input's DOM node
Scroll to sectionThe section's DOM node
Timer / intervalThe id from setInterval
Video play/pauseThe video element
Third-party libraryThe container node for a chart or map

4. How does it work?

The journey of a DOM ref
useRef(null) creates an empty box
You attach the element to it with the ref attribute in JSX
After React builds the DOM, it puts the node in ref.current
Use ref.current in an event handler or an effect
On unmount, React sets ref.current back to null

The ref object stays the same on every render. That is why its value is not lost between renders.

5. Code example

Input focus

jsx
import { useRef } from "react";

export default function SearchBar() {
  const inputRef = useRef(null);

  return (
    <>
      <input ref={inputRef} placeholder="Search..." />
      <button onClick={() => inputRef.current.focus()}>Focus</button>
    </>
  );
}

Remembering a timer id

jsx
import { useRef, useState } from "react";

export default function Stopwatch() {
  const [seconds, setSeconds] = useState(0);
  const intervalRef = useRef(null);

  function start() {
    clearInterval(intervalRef.current);
    intervalRef.current = setInterval(() => setSeconds((s) => s + 1), 1000);
  }

  function stop() {
    clearInterval(intervalRef.current);
  }

  return (
    <>
      <p>{seconds}s</p>
      <button onClick={start}>Start</button>
      <button onClick={stop}>Stop</button>
    </>
  );
}

React 19: ref is a normal prop

jsx
import { useRef } from "react";

function MyInput({ label, ref }) {
  return (
    <label>
      {label}
      <input ref={ref} />
    </label>
  );
}

function Form() {
  const nameRef = useRef(null);
  return <MyInput label="Name" ref={nameRef} />;
}

How does the code work?

PartMeaning
useRef(null)Creates the box, with current starting as null
ref={inputRef}React puts the DOM node into inputRef.current
intervalRef.current = ...A direct assignment, with no re-render
{ label, ref }In React 19, ref arrives like any other prop

In older React, you needed forwardRef to pass a ref to a child. React 19 does not need it.

6. Common mistakes

Mistake: Reading or writing ref.current during render

Why? Changing a ref does not re-render, so the UI can show an old value, and the render becomes impure.

Better: Use refs in event handlers and effects. Keep any value that appears on screen in state.

Mistake: Keeping UI values in a ref, like a counter

Why? The value changes but the screen does not update.

Better: Keep anything that is rendered in useState, and use a ref only for behind-the-scenes data.

Mistake: Calling ref.current.focus() during the first render

Why? During the first render the DOM does not exist yet, so ref.current is null and you get an error.

Better: Work with the DOM in an event handler or useEffect, once the node is attached.

Interview tip

"What is the difference between useRef and useState?" Both remember a value between renders. The difference is that changing state causes a re-render, and changing a ref does not.

7. What to learn before and after

Final quick revision

PointDetail
ReturnsA { current: value } object
Re-renderDoes not happen
DOM accessThe ref attribute gives you the node
Where to useHandlers and effects, not render
React 19ref is a normal prop, no forwardRef needed