Skip to content

Commit b60d0a1

Browse files
authored
Make changes more focused
1 parent 1416468 commit b60d0a1

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/content/reference/react/useImperativeHandle.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Call `useImperativeHandle` at the top level of your component to customize the r
2525
```js
2626
import { useImperativeHandle } from 'react';
2727

28-
function MyInput({ ref, ...props }) {
28+
function MyInput({ ref }) {
2929
useImperativeHandle(ref, () => {
3030
return {
3131
// ... your methods ...
@@ -45,9 +45,8 @@ function MyInput({ ref, ...props }) {
4545
* **optional** `dependencies`: The list of all reactive values referenced inside of the `createHandle` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. If a re-render resulted in a change to some dependency, or if you omitted this argument, your `createHandle` function will re-execute, and the newly created handle will be assigned to the ref.
4646
4747
<Note>
48-
In versions prior to React 19, components by default don't expose their DOM nodes to parent components. This was done by opting in with [`forwardRef`](/reference/react/forwardRef) for the parent component to [have access](/learn/manipulating-the-dom-with-refs) to the child's DOM node.
4948
50-
`forwardRef` is now depreciated. Read the blog on [`ref` as a prop](/blog/2024/12/05/react-19#ref-as-a-prop) to learn more.
49+
Before React 19, it was necessary to use [`forwardRef`](/reference/react/forwardRef) to get the `ref`. Starting with React 18, [`ref` is available a prop.](/blog/2024/12/05/react-19#ref-as-a-prop)
5150
5251
</Note>
5352
@@ -64,8 +63,8 @@ In versions prior to React 19, components by default don't expose their DOM node
6463
To expose a DOM node to the parent element, pass in the `ref` prop to the node.
6564
6665
```js {2}
67-
function MyInput({ ref, ...props }) {
68-
return <input {...props} ref={ref} />;
66+
function MyInput({ ref }) {
67+
return <input ref={ref} />;
6968
};
7069
```
7170
@@ -74,25 +73,25 @@ With the code above, [a ref to `MyInput` will receive the `<input>` DOM node.](/
7473
```js {4-8}
7574
import { useImperativeHandle } from 'react';
7675

77-
function MyInput({ ref, ...props }) {
76+
function MyInput({ ref }) {
7877
useImperativeHandle(ref, () => {
7978
return {
8079
// ... your methods ...
8180
};
8281
}, []);
8382

84-
return <input {...props} />;
83+
return <input />;
8584
};
8685
```
8786
88-
Note that in the code above, the `ref` is no longer forwarded to the `<input>`.
87+
Note that in the code above, the `ref` is no longer passed to the `<input>`.
8988
9089
For example, suppose you don't want to expose the entire `<input>` DOM node, but you want to expose two of its methods: `focus` and `scrollIntoView`. To do this, keep the real browser DOM in a separate ref. Then use `useImperativeHandle` to expose a handle with only the methods that you want the parent component to call:
9190
9291
```js {7-14}
9392
import { useRef, useImperativeHandle } from 'react';
9493

95-
function MyInput({ ref, ...props }) {
94+
function MyInput({ ref }) {
9695
const inputRef = useRef(null);
9796

9897
useImperativeHandle(ref, () => {
@@ -106,7 +105,7 @@ function MyInput({ ref, ...props }) {
106105
};
107106
}, []);
108107

109-
return <input {...props} ref={inputRef} />;
108+
return <input ref={inputRef} />;
110109
};
111110
```
112111
@@ -204,7 +203,7 @@ import { useRef, useImperativeHandle } from 'react';
204203
import CommentList from './CommentList.js';
205204
import AddComment from './AddComment.js';
206205

207-
function Post({ ref, ...props }) {
206+
function Post({ ref }) {
208207
const commentsRef = useRef(null);
209208
const addCommentRef = useRef(null);
210209

@@ -235,7 +234,7 @@ export default Post;
235234
```js src/CommentList.js
236235
import { useRef, useImperativeHandle } from 'react';
237236

238-
function CommentList({ ref, ...props }) {
237+
function CommentList({ ref }) {
239238
const divRef = useRef(null);
240239

241240
useImperativeHandle(ref, () => {
@@ -265,7 +264,7 @@ export default CommentList;
265264
```js src/AddComment.js
266265
import { useRef, useImperativeHandle } from 'react';
267266

268-
function AddComment({ ref, ...props }) {
267+
function AddComment({ ref }) {
269268
return <input placeholder="Add comment..." ref={ref} />;
270269
}
271270

0 commit comments

Comments
 (0)