React Developer Interview Question

When do you use useRef instead of useState?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

Use useRef when you need a mutable value that survives renders but must not cause one: a DOM node, a timer id, the previous value of a prop, a flag tracking whether something already ran. Use useState when the value should show up in the UI, because writing to a ref does not schedule a render. The rule of thumb is simple: if the screen should change, it is state.

Why interviewers ask this

This is a quick read on whether you understand the render cycle. The interviewer wants to hear the render triggering difference first, then correct instincts about when a ref is the right tool. They will often follow up on reading or writing refs during render, which is where candidates who only use refs for DOM nodes tend to fall over, and where the mutable versus rendered distinction matters.

How to structure your answer

  • Lead with the difference that matters: refs do not trigger renders.
  • List the three or four legitimate uses of a ref.
  • Give the rule of thumb for choosing.
  • Mention that refs should not be read or written during render.

Example answer

Spoken example, first person

The dividing line is whether changing the value should repaint the screen. State does, a ref does not. So a ref is where I put anything that is bookkeeping rather than UI: the DOM node I need to focus or measure, an interval id I have to clear on unmount, the last value of a prop so I can compare, a did I already do this flag. The classic mistake is storing something in a ref and wondering why the UI never updates, and the mirror image is putting a scroll position in state and re-rendering on every scroll event. I also keep the rule that I do not read or write refs during render, only in effects and event handlers, because during render React expects the function to be pure and a ref read can give you different values across passes. One nice change in React 19 is that ref is just a normal prop for function components now, so I stopped wrapping things in forwardRef.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • How would you get the previous value of a prop?
  • Why should you not read or write a ref during render?
  • How do you attach a ref to a child component in React 19?

Related react developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot