Course contentsShow
← React
Lesson 710 of 2,39619. useRefFree lesson

When NOT to Use useRef for Values

If changing a value should trigger a re-render or affect what's displayed, use useState instead—refs are invisible to React.

When NOT to Use useRef for Values

You'll learn: When reaching for useRef will silently break your UI and why useState is the right choice instead.

The Critical Distinction

useRef is perfect for values you want to persist but that don't affect what's displayed. The moment a value controls what appears on screen or should trigger a re-render when it changes, useRef becomes the wrong tool.

Why Refs Are Invisible to React

Think of refs like a secret notepad you keep in your pocket. You can write notes, read them, update them—but nobody else knows you've changed anything. React doesn't watch your notepad.

When you update ref.current = newValue, React says "cool, noted" and moves on. No re-render happens. Your component doesn't re-run. Your JSX doesn't update.

The Broken Pattern

// ❌ WRONG: UI won't update when count changes
function BrokenCounter() {
  const count = useRef(0);

  return (
    <div>
      <p>Count: {count.current}</p>
      <button onClick={() => count.current += 1}>
        Increment
      </button>
    </div>
  );
}

Clicking the button does increment count.current, but the screen shows the same number forever. React never knows to re-render.

When to Use useState Instead

If the value:

  • Appears in your JSX (<p>{value}</p>)
  • Controls conditional rendering ({value && <Something />})
  • Determines which components render (value === 'tab1' ? <Tab1 /> : <Tab2 />)
  • Affects props you pass down

Use useState. You need React to re-render when it changes.

Key Takeaway: Refs are for values that shouldn't trigger re-renders (timers, DOM nodes, previous values for comparison). If changing a value should update the UI, useState is mandatory—refs will silently fail to display changes.