You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react/useImperativeHandle.md
+12-13Lines changed: 12 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ Call `useImperativeHandle` at the top level of your component to customize the r
25
25
```js
26
26
import { useImperativeHandle } from'react';
27
27
28
-
functionMyInput({ ref, ...props }) {
28
+
functionMyInput({ ref }) {
29
29
useImperativeHandle(ref, () => {
30
30
return {
31
31
// ... your methods ...
@@ -45,9 +45,8 @@ function MyInput({ ref, ...props }) {
45
45
* **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.
46
46
47
47
<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.
49
48
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)
51
50
52
51
</Note>
53
52
@@ -64,8 +63,8 @@ In versions prior to React 19, components by default don't expose their DOM node
64
63
To expose a DOM node to the parent element, pass in the `ref` prop to the node.
65
64
66
65
```js {2}
67
-
functionMyInput({ ref, ...props }) {
68
-
return<input {...props} ref={ref} />;
66
+
functionMyInput({ ref }) {
67
+
return<input ref={ref} />;
69
68
};
70
69
```
71
70
@@ -74,25 +73,25 @@ With the code above, [a ref to `MyInput` will receive the `<input>` DOM node.](/
74
73
```js {4-8}
75
74
import { useImperativeHandle } from'react';
76
75
77
-
functionMyInput({ ref, ...props }) {
76
+
functionMyInput({ ref }) {
78
77
useImperativeHandle(ref, () => {
79
78
return {
80
79
// ... your methods ...
81
80
};
82
81
}, []);
83
82
84
-
return<input {...props} />;
83
+
return<input />;
85
84
};
86
85
```
87
86
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>`.
89
88
90
89
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:
0 commit comments