Skip to content

Commit a945321

Browse files
committed
remove unused props and optional chaining, explain crash
1 parent 64da1ae commit a945321

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/content/learn/manipulating-the-dom-with-refs.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ export default function MyForm() {
360360
const inputRef = useRef(null);
361361

362362
function handleClick() {
363-
inputRef.current?.focus();
363+
inputRef.current.focus();
364364
}
365365

366366
return (
@@ -376,13 +376,15 @@ export default function MyForm() {
376376

377377
</Sandpack>
378378

379+
Instead, the application crashes because `inputRef.current` is `null`, due to `<MyInput />` not passing the `ref` prop down to one of its children.
380+
379381
This happens because by default React does not let a component access the DOM nodes of other components. Not even for its own children! This is intentional. Refs are an escape hatch that should be used sparingly. Manually manipulating _another_ component's DOM nodes makes your code even more fragile.
380382

381383
Instead, components that _want_ to expose their DOM nodes have to **opt in** to that behavior. A component can specify that it "forwards" its ref to one of its children by passing the `ref` prop down.
382384

383385
```js
384-
function MyInput({ ref, ...props }) {
385-
return <input {...props} ref={ref} />;
386+
function MyInput({ ref }) {
387+
return <input ref={ref} />;
386388
}
387389
```
388390

@@ -398,8 +400,8 @@ Now clicking the button to focus the input works:
398400
```js
399401
import { useRef } from 'react';
400402

401-
function MyInput({ ref, ...props }) {
402-
return <input {...props} ref={ref} />;
403+
function MyInput({ ref }) {
404+
return <input ref={ref} />;
403405
}
404406

405407
export default function Form() {
@@ -435,15 +437,15 @@ In the above example, `MyInput` exposes the original DOM input element. This let
435437
```js
436438
import { useRef, useImperativeHandle } from 'react';
437439

438-
function MyInput({ ref, ...props }) {
440+
function MyInput({ ref }) {
439441
const realInputRef = useRef(null);
440442
useImperativeHandle(ref, () => ({
441443
// Only expose focus and nothing else
442444
focus() {
443445
realInputRef.current.focus();
444446
},
445447
}));
446-
return <input {...props} ref={realInputRef} />;
448+
return <input ref={realInputRef} />;
447449
}
448450

449451
export default function Form() {

src/content/reference/react/useRef.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ Sometimes, you may want to let the parent component manipulate the DOM inside of
455455
```js
456456
import { useRef } from 'react';
457457

458-
function MyInput({ ref, ...props }) {
459-
return <input {...props} ref={ref} />;
458+
function MyInput({ ref }) {
459+
return <input ref={ref} />;
460460
}
461461

462462
export default function Form() {

0 commit comments

Comments
 (0)