Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 8.3 — Passing Refs to Your Components
React · Example 1 of 3

React 19: ref is just a prop

MyInput receives ref in its props and passes it to the <input>. The parent's ref ends up holding the real input, so Edit name can focus it.

Since React 19 a function component receives ref as an ordinary prop and can pass it on to a DOM element; older code wraps the component in forwardRef instead, and ref callbacks can now return a cleanup function.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a Button component that accepts every normal button prop plus ref (React 19 style), and a parent that focuses it on mount.

02

Convert this to React 19 style: const Search = forwardRef((props, ref) => <input ref={ref} type="search" {...props} />);

Explain it without notes

01

How do you let a parent get a ref to a DOM element inside your component in React 19? How was it done before?

02

What changed about callback refs in React 19?

03

Why does an inline callback ref run on every render, and when does that matter?

04

Why are a component's DOM nodes private by default?

React 19: ref is just a prop

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

MyInput props: [ 'ref', 'label', 'id' ]
focused: <input#name>