midReact
What is useRef and when should you use it?
Updated Apr 23, 2026
Short answer
In React, `useRef` is a hook that returns a mutable object that persists across renders without causing a re-render when updated.
You should use useRef when you need to:
- Access DOM elements directly
- Store values that don’t affect rendering (like timers, previous values, or flags)
Deep explanation
What is useRef?
useRef returns an object like this:
TypeScript
const ref = { current: initialValue }This object:
- Persists for the entire lifecycle of the component
- Does NOT trigger a re-render when
currentchanges
---
Basic Syntax
TypeScript
import { useRef } from "react";function Example() { const inputRef = useRef(null); return <input ref={inputRef} />;}---
1\. Accessing DOM Elements
One of the most common uses is directly accessing DOM nodes.
Example: focusing an input…
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro