Skip to content
Guest
Learn React · Course Syllabus Show lessons
Performance & DOM · 8 min read · Intermediate

useRef & Direct DOM Access

Store mutable values that do not trigger re-renders (timer IDs) and imperatively focus DOM elements with useRef.

What you'll learn

  • Distinguish useRef (mutating .current does NOT re-render) from useState
  • Attach ref={inputRef} to focus inputs or measure DOM dimensions
  • Store interval IDs or previous values cleanly across renders

useRef(initial) returns a persistent box { current: initial } that survives across re-renders. Unlike useState, changing ref.current does not trigger a re-render, making it ideal for DOM element handles (inputRef.current?.focus()) and timer IDs.

Fill-in-the-Blank Exercise

Focus an Input via Ref

Create inputRef with useRef and call .focus() on inputRef.current.

const inputRef = (null);
const focusBox = () => inputRef.?.focus();

Live Focus Ref Demo

Imperative Focus Handle
Live Result
Console Output

No console output yet. Try calling console.log(...) in JS.

Check Your Understanding

Score: 0 / 1

1. What happens in React when you update ref.current = 42?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind