Repository navigation
useRef
vinibiavatti1 edited this page Sep 28, 2026
·
3 revisions
Returns a mutable ref object whose .current property is initialized to the passed argument. It persists across renders and updating it does not trigger a re-render.
Stores mutable data across renders without triggering a re-render when its value changes.
import { useRef } from 'react';
export const MyComponent = () => {
const countRef = useRef(0);
return (
<button onClick={() => { countRef.current += 1 }}>Increment</button> // Does not trigger re-render
)
};Stores a reference directly to a DOM element to interact with it imperatively.
import { useRef } from 'react';
export const MyComponent = () => {
const inputRef = useRef(null);
useEffect(() => { inputRef.current?.focus(); }, []); // Access element imperatively
return (
<input ref={inputRef} />
)
};- Home
- Functional Components
- Props
- Fragments
- Memoization
- Forward Ref
- Portals
- Suspense
-
Hooks
- State
- Context
- Effect
- Ref
- Performance
- Concurrent
- Form
- Utility
- Custom Hooks