useRef
The React hook that creates a ref object, { current: initialValue }, which stays the same object for the life of the component.
In more detail
Changing ref.current does not trigger a render, which makes it right for values that do not affect the display, such as a click counter used only in handlers or an interval ID. createRef makes a ref outside components, for example in a class or a test.
Example
const inputRef = useRef<HTMLInputElement>(null);Related terms
- ref — A React handle that points at a real DOM element, so code can focus it, measure it or attach listeners.
- component state — Data a component remembers between renders, such as typed text or whether a menu is open; changing it makes React render again.
- mutation — A change to data, and in TanStack Query or tRPC specifically, a request that creates, updates or deletes data on the server.
Taught in
- useLayoutEffect vs useEffect: Painting And Synchronous Reads — React 19 & Modern Component Architecture
- useRef Mechanics: DOM References And Mutable Persistent State — React 19 & Modern Component Architecture
- useImperativeHandle: Controlled Ref Forwarding Boundaries — React 19 & Modern Component Architecture
- Creating Event Listener Hooks with Clean Teardown Logic — React 19 & Modern Component Architecture