The useRef hook provides a way to persist values across renders without causing a re-render. It is commonly used to access DOM elements or store mutable values.
useRef?Returns a mutable object with a .current property
Value persists between renders
Updating .current does not trigger a re-render
One of the most common use cases.
Value changes
Component does not re-render
useRef vs useStateuseRef | useState |
|---|---|
| No re-render | Triggers re-render |
| Mutable | Immutable updates |
| DOM access | UI data |
| Persistent storage | Reactive state |
useRef with Timers
Allows parent components to access child DOM nodes.
❌ Using useRef instead of state for UI updates
❌ Expecting re-render on ref change
❌ Overusing refs
Use refs for DOM access
Use state for UI logic
Keep ref usage minimal
Avoid mutating UI-related data
useRef stores mutable values
Does not cause re-render
Ideal for DOM and timers
Persists across renders
Take quizzes related to this topic and see where you stand!
Start Quiz Now