Skip to content

Refs: Values That Don't Re-render

The Course
•
20 min study

A box React doesn't watch

Sometimes a component needs to remember something that isn't shown: a timer's id, the previous value of a prop, how many times something happened. State would work, but changing state re-renders the component, which is wasted work when nothing on screen changes. For these, React has refs:

JavaScript
const intervalRef = useRef(null);

useRef returns a plain object, { current: null }. It's the same object on every render. Read and change intervalRef.current in event handlers or effects, rather than during render. Changing it does not re-render the component.

A focus timer React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useRef, useState } from "react"; export default function App() { const [elapsed, setElapsed] = useState(0); const [running, setRunning] = useState(false); const intervalRef = useRef(null); const lapsRef = useRef(0); function start() { setRunning(true); intervalRef.current = setInterval(() => { setElapsed((e) => e + 1); }, 1000); } function stop() { setRunning(false); clearInterval(intervalRef.current); } function lap() { lapsRef.current = lapsRef.current + 1; console.log("laps is now", lapsRef.current); } return ( <main> <h1>Focus timer: {elapsed} s</h1> <div className="toolbar"> {running ? <button onClick={stop}>Stop</button> : <button onClick={start}>Start</button>} <button onClick={lap}>Lap</button> </div> <p className="muted">Laps: {lapsRef.current}</p> </main> ); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Press Start, wait, then Stop. start stores the interval's id in intervalRef.current, and stop reads it back to clear it. The id never appears on screen, so it doesn't need to be state. Being a ref also means it survives every re-render the ticking causes:

JavaScript
function start() {
  setRunning(true);
  intervalRef.current = setInterval(() => {
    setElapsed((e) => e + 1);
  }, 1000);
}

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

Predict: a ref on screen

While the timer is stopped, press Lap three times. Watch the console and the Laps line. Then press Start.

Check your prediction

The console counts up, laps is now 1, 2, 3, but the page still says Laps: 0. Changing a ref doesn't re-render, so the screen shows whatever lapsRef.current was during the last render. Press Start, and the first tick re-renders the component: Laps: 3 appears suddenly:

JavaScript
function lap() {
  lapsRef.current = lapsRef.current + 1;
  console.log("laps is now", lapsRef.current);
}

That's the rule in action: if it's shown on screen, it's state. Refs are for values the screen doesn't depend on. Reading or writing ref.current during render, as the Laps line does, is a bug waiting to happen. Read and write refs in event handlers and effects.

State or ref?

StateRef
Changing it re-rendersYesNo
Value during a renderA snapshot, fixed for that renderWhatever it is right now
Use forAnything shown on screenTimer ids, DOM elements, values the screen doesn't show

Your turn

Exercise

Fix the laps

Make Laps update straight away when you press Lap, stopped or running.

Your working space React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useRef, useState } from "react"; export default function App() { const [elapsed, setElapsed] = useState(0); const [running, setRunning] = useState(false); const intervalRef = useRef(null); const lapsRef = useRef(0); function start() { setRunning(true); intervalRef.current = setInterval(() => { setElapsed((e) => e + 1); }, 1000); } function stop() { setRunning(false); clearInterval(intervalRef.current); } function lap() { lapsRef.current = lapsRef.current + 1; console.log("laps is now", lapsRef.current); } return ( <main> <h1>Focus timer: {elapsed} s</h1> <div className="toolbar"> {running ? <button onClick={stop}>Stop</button> : <button onClick={start}>Start</button>} <button onClick={lap}>Lap</button> </div> <p className="muted">Laps: {lapsRef.current}</p> </main> ); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea

The value the user must see immediately belongs in state. Changing a ref alone cannot request a render.

Hint 2: a first step

It's shown on screen, so it should be state, not a ref.

Show one solution
JavaScript
const [laps, setLaps] = useState(0);

function lap() {
  setLaps(laps + 1);
}
JSX
<p className="muted">Laps: {laps}</p>

Delete lapsRef. The interval id stays a ref: nothing on screen shows it.

Was this article helpful?

Your response is saved on this device.