Skip to content
tezvyn:

React Refs: Memory Without Re-renders

Source: react.devHardHow cards are made

React Refs: Memory Without Re-renders

A React ref is like a secret pocket for your component to remember information without triggering re-renders. Use it to hold values not needed for rendering, like a timer ID. The footgun: changing a ref's value won't update the UI; use state for that.

Why it exists

Components often need to remember information that doesn't directly affect the visual output. Using state for this is inefficient because it would cause unnecessary re-renders every time the value changes. Refs provide a way to store mutable values that persist across renders without triggering them.

The mental model

A ref is a plain JavaScript object with a single current property. Think of it as a box you can put anything in—a number, an object, or a timer ID. You can read from and write to this box directly, but React doesn't watch it. When you change what's inside, React doesn't re-render your component. It's a secret pocket, separate from the main rendering lifecycle.

How it works

You create a ref by calling the useRef hook and passing an initial value: const myRef = useRef(initialValue). This returns an object like { current: initialValue }. To access or update the value, you use myRef.current. For example, myRef.current = myRef.current + 1. This change is synchronous and mutable, but it will not cause your component to re-render. The value is preserved between renders, just like state.

When to use it

Use a ref when you need to store a value that isn't used for rendering. A classic case is storing an ID returned by a browser API like setInterval. You need to hold onto the ID to clear the timer later (e.g., in an event handler), but the ID itself is never displayed on the screen. Storing it in a ref avoids needless re-renders.

When not to use it

Do not use refs for data that you expect to be reflected in the UI. If a value changes and you want the user to see that change, it must be in state. The biggest mistake is assuming a ref update will refresh the UI. If the screen needs to change based on a value, that value belongs in state.

One canonical example

Building a stopwatch. You use state for the start time and current time because they are displayed on screen as secondsPassed. When you start the timer with setInterval, it returns an interval ID. This ID is needed to stop the timer later with clearInterval. You store this ID in a ref (const intervalRef = useRef(null)). The ID itself is never rendered, so it doesn't need to be state. This correctly separates rendering concerns (state) from internal logic management (refs).

Interview question

In a React component, for which scenario would using a ref be the most appropriate choice over state?

  • a.Holding an interval ID returned by setInterval that needs to be cleared later.Correct
  • b.Storing a user's input that directly updates a displayed text field.
  • c.Storing a list of items that are rendered dynamically on the screen.
  • d.Managing a boolean flag that toggles the visibility of an element.
Why?

Refs are ideal for storing mutable values that persist across renders without triggering UI updates, such as an interval ID that is never displayed. Options A, C, and D all involve data that directly affects the UI, requiring state to ensure re-renders when the values change.

Just read this? Test yourself on what you have been reading.

Read the original → react.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles