Skip to content

useRef mutation does not trigger re-render

Quick summary

You updated ref.current but the component did not re-render — useRef mutations are not tracked by React and do not trigger updates.

Why this happens

  • You used useRef to store a value that should be reactive state, but ref changes do not cause re-renders

  • You mutated ref.current and expected the UI to update, but React does not observe ref changes

  • You mixed up useState and useRef semantics, using a ref for data that drives rendering

Minimal example

✗ Broken code
function Timer() {
  const seconds = useRef(0);
  useEffect(() => {
    const id = setInterval(() => {
      seconds.current++; // no re-render!
    }, 1000);
    return () => clearInterval(id);
  }, []);
  return <div>{seconds.current}</div>; // always shows 0
}
✓ Fixed code
function Timer() {
  const [seconds, setSeconds] = useState(0);
  useEffect(() => {
    const id = setInterval(() => {
      setSeconds(s => s + 1); // triggers re-render
    }, 1000);
    return () => clearInterval(id);
  }, []);
  return <div>{seconds}</div>; // updates every second
}

Using useState instead of useRef makes the value reactive — each update triggers a re-render and the UI reflects the new value.

How to diagnose

  • Log the ref.current value to confirm it changed, then check if the UI updated to verify the disconnect

  • Review whether the value should trigger a re-render — if yes, it should be useState not useRef

  • Check if the component re-renders by adding a console.log at the top of the render function

How to fix

  • Switch from useRef to useState for values that should trigger re-renders when they change

  • Force a re-render after mutating the ref by calling a dummy state setter: const [, force] = useState(0); force(x => x + 1)

  • Keep the ref for mutable storage but also maintain a state mirror that updates when you need the UI to reflect the change

How to prevent

  • Use useRef only for values that do not need to trigger re-renders (DOM references, timers, mutable containers)

  • Use useState for any value that drives rendering — never use a ref as a reactive state substitute

Related Resources

Related Glossary

Related Lessons

← Back to language