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.
import { useRef } from 'react';
// Scenario A: Store mutable data (does not trigger re-render)
const Component1 = () => {
const countRef = useRef(0);
return (
<button onClick={() => { countRef.current += 1 }}>Increment</button>
)
};
// Scenario B: Store reference to an element
const Component2 = () => {
const inputRef = useRef(null);
useEffect(() => { inputRef.current?.focus(); }, []);
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