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

The Key Difference: useRef vs useState

Changing ref.current does not cause a re-render, while setState always does—this is the fundamental distinction.

The Key Difference: useRef vs useState

You'll learn the fundamental distinction that makes useRef and useState serve completely different purposes.

The Core Difference

When you change a ref's .current property, React doesn't re-render your component. When you call a state setter function from useState, React always triggers a re-render.

This is the single most important distinction between these two tools.

function Example() {
  const countRef = useRef(0);
  const [countState, setCountState] = useState(0);

  const handleRefClick = () => {
    countRef.current += 1;
    // Component does NOT re-render
    // UI won't update to show new value
    console.log('Ref:', countRef.current);
  };

  const handleStateClick = () => {
    setCountState(countState + 1);
    // Component WILL re-render
    // UI updates to show new value
  };

  return (
    <div>
      <p>Ref count: {countRef.current}</p>
      <p>State count: {countState}</p>
      <button onClick={handleRefClick}>Increment Ref</button>
      <button onClick={handleStateClick}>Increment State</button>
    </div>
  );
}

When you click "Increment Ref", the ref value changes but the screen shows the old number—no re-render happened. When you click "Increment State", the screen updates immediately because setCountState triggered a re-render.

Think of It This Way

useState is like a public announcement system—every change broadcasts to React, "Something changed! Update the screen!"

useRef is like a private notebook—you can write whatever you want in it, but React never reads it or reacts to it.

Key Takeaway: Changing ref.current never causes re-renders; calling setState always does. Use refs for values you need to remember but don't need to display, and state for values that should affect what users see.