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

The ref.current Timing Guarantee

ref.current is always synchronously up-to-date, unlike state which batches updates—use this for immediate mutable storage.

The ref.current Timing Guarantee

What you'll learn: Why ref.current is always immediately up-to-date, unlike state updates which batch and delay.

The Core Idea

When you update ref.current, the change happens synchronously and immediately—right on that line of code. There's no batching, no scheduling, no waiting for the next render. This is fundamentally different from setState, which queues updates and applies them later.

Think of it like writing on a whiteboard vs. sending a letter. With ref.current, you're holding the marker—you write something and it's instantly there. With setState, you're dropping a letter in the mailbox—React will deliver it, but on its own schedule.

Why This Matters

Because refs update synchronously, you can:

  • Read the value immediately after writing it:

    countRef.current = 5;
    console.log(countRef.current); // Always logs 5, instantly
    
  • Use refs for values that must be current right now:

    function handleClick() {
      timeoutRef.current = setTimeout(() => {
        // Logic here
      }, 1000);
      // timeoutRef.current is ALREADY the timeout ID, no waiting
    }
    

Compare this to state:

setCount(5);
console.log(count); // Still the OLD value! Update happens later

When to Leverage This

Use refs when you need immediate mutable storage that doesn't trigger re-renders—tracking timers, storing DOM nodes, keeping the latest callback reference, or maintaining flags that must be current right now.

Key Takeaway: ref.current updates synchronously on the same line, making it perfect for values you need to read/write immediately without waiting for React's render cycle.