Skip to content
Learn Netverks

Lesson

Step 32/36 89% through track

refs-dom

Refs and the DOM

Last reviewed Jun 1, 2026 Content v20260601
Track mode
client_react
Means
In-browser React TSX
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Refs and the DOM: the concepts, APIs, and habits you need before advancing in React.

Without Refs and the DOM, you will struggle to read or extend React codebases and playground exercises.

You will apply Refs and the DOM in contexts like: SPAs, dashboards, design-system-driven products, and React Native mobile apps.

Write TypeScript/TSX, click Run in browser—React 18 loads from CDN, JSX compiles in the tab, UI renders in the preview root, and printOutput feeds the terminal.

Toward the end of the track—consolidate before capstone-style review lessons.

Refs hold mutable values that survive re-renders without triggering updates—DOM nodes, timer IDs, previous prop snapshots. Use them when you need imperative access: focus an input, measure width, integrate a non-React chart library.

useRef

const inputRef = React.useRef(null);
React.useEffect(() => {
  inputRef.current?.focus();
}, []);
return <input ref={inputRef} />;

Ref vs state

  • Changing ref.current does not re-render
  • State changes schedule a render—use for UI that must update on screen

Callback refs

ref={(node) => ...} runs when the node mounts/unmounts—handy when parent and child need the same element reference dynamically.

Important interview questions and answers

  1. Q: Can you store anything in a ref?
    A: Yes—DOM nodes, timers, or arbitrary mutable boxes; avoid storing derived UI that should be state.
  2. Q: Why not use document.querySelector in components?
    A: Breaks with multiple instances and SSR; refs tie behavior to the correct element instance.

Self-check

  1. When should focus management use a ref?
  2. Why does updating a ref not re-render the component?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ref vs state?
  • DOM access last resort?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump