Skip to content

useRef

In one line

useRef is a Hook that returns a mutable ref object whose .current property persists across renders without causing re-renders.

In simple words

useRef gives you a mutable container that holds a single value in its .current property. Unlike state, changing .current does not trigger a re-render. The value persists across renders.

There are two main uses: (1) accessing DOM elements directly — pass the ref to a DOM element's ref attribute and React sets .current to the DOM node; (2) storing any mutable value (a timer ID, a previous value, a "is mounted" flag) that you need to persist but don't want to cause re-renders.

Think of refs as instance variables for function components. They are an escape hatch when you need to step outside React's reactive model. Avoid overusing them — if a value should affect the UI, it should be state, not a ref.

Example

jsx
function FocusInput() {
  const inputRef = useRef(null);

  return (
    <>
      <input ref={inputRef} />
      <button onClick={() => inputRef.current.focus()}>
        Focus the input
      </button>
    </>
  );
}

inputRef.current points to the DOM <input> element after mount. The button uses it to call .focus() imperatively.

Related terms

Related Resources

Related Lessons

  • useRef

    Learn useRef for DOM access and mutable values.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary