useRef is a React Hook that allows you to create a mutable reference object. This reference object can hold a value that persists across renders and changing its .current value does NOT trigger a re-render. It's also commonly used to get direct access to DOM elements. What is useRef? useRef is a React hook returning a mutable ref object with a single .current property. const myRef = useRef(initialValue); // myRef looks like: { current: initialValue } Initialization: The initialValue you provide is used to set myRef.current only during the component's initial render. It's ignored on subsequent renders. If you omit the argument, initialValue defaults to undefined. Return Value: useRef returns the same ref object on every render of a given component instance. This is how it persists across renders.

Apr 6, 2025 - 08:37
 0

useRef is a React Hook that allows you to create a mutable reference object. This reference object can hold a value that persists across renders and changing its .current value does NOT trigger a re-render. It's also commonly used to get direct access to DOM elements.

What is useRef?

useRef is a React hook returning a mutable ref object with a single .current property.

const myRef = useRef(initialValue);
// myRef looks like: { current: initialValue }
  • Initialization: The initialValue you provide is used to set myRef.current only during the component's initial render. It's ignored on subsequent renders. If you omit the argument, initialValue defaults to undefined.

  • Return Value: useRef returns the same ref object on every render of a given component instance. This is how it persists across renders.