Refs Don't Trigger Re-renders
What you'll learn: Why mutating ref.current never causes your component to re-render, making refs perfect for values React doesn't need to "react" to.
The Core Difference
When you change state with setState, React schedules a re-render to reflect that new value in the UI. But when you change ref.current, absolutely nothing happens—no re-render, no update cycle, nothing. React doesn't watch refs at all.
This is by design. Refs are for imperative values: data you need to track or manipulate, but that shouldn't trigger any visual updates.
Why This Matters
Remember, every re-render means React recalculates your component's JSX, reconciles the virtual DOM, and potentially updates the real DOM. That's work. If you're storing something like:
- A timer ID
- A scroll position you'll check later
- A previous value for comparison
- A DOM node reference
...you don't want React re-rendering every time that value changes. You just want to update the value and move on.
Analogy: Post-it Note vs. Announcement
State is like announcing something over a loudspeaker—everyone hears it, everyone reacts, the whole room responds.
Refs are like quietly updating a Post-it note on your desk. You changed it, you know it changed, but nobody else needs to care or react to it.
The Practical Implication
When you do inputRef.current = someValue or inputRef.current.focus(), you're directly manipulating something outside React's awareness. React won't re-render, which means if you need the UI to reflect a ref's value, refs are the wrong tool—use state instead.
Key Takeaway: Mutating ref.current is invisible to React's render cycle—use refs for values you need to track imperatively without triggering re-renders.