Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
6a35e13
Update react-19-upgrade-guide.md
sophiebits Dec 2, 2024
4bae717
Fix typo in react-19-upgrade-guide.md
sophiebits Dec 2, 2024
fd8b1e0
Update React v19 blog post for stable release (#7321)
rickhanlonii Dec 5, 2024
4673a05
Merge v19 docs to main (#7322)
rickhanlonii Dec 5, 2024
d6f3659
update version label
rickhanlonii Dec 5, 2024
d2536c0
fix vv19
rickhanlonii Dec 5, 2024
8235ae9
Fix types install instructions in 19 upgrade guide
eps1lon Dec 5, 2024
4d7cc91
Update Versions page (#7324)
rickhanlonii Dec 5, 2024
ebedc89
fix links to prerender docs (#7323)
mayank99 Dec 5, 2024
acda167
Add compiler beta release to Version changelog
rickhanlonii Dec 5, 2024
69edd84
fixed typo in yarn add command (#7326)
JakeSaterlay Dec 5, 2024
3b02f82
Update React DevTools docs to reference RN DevTools (#7355)
huntie Dec 11, 2024
9fb491d
Fix troubleshooting links in startTransition reference (#7367)
jimmycallin Dec 16, 2024
51864f6
(fix): mismatch href bug (#7368)
ahm3tozenir Dec 16, 2024
bc93f05
Fix stylesheet precedence example (#7235)
EricCote Dec 16, 2024
07eca83
Fix intro of prerenderToNodeStream (#7356)
smikitky Dec 16, 2024
c92bad2
chore: several major typo fixes (#7362)
coalio Dec 16, 2024
d3bd0f9
Update act.md (#7363)
huiliangShen Dec 16, 2024
31456db
chore: correct typo in useTransition docs (#7366)
AminDannak Dec 16, 2024
d4d1683
chore: fix a typo in startTransition docs (#7361)
AminDannak Dec 16, 2024
04ba43c
fix: add link to prop-type codemod (#7344)
renbaoshuo Dec 16, 2024
4349dd5
fix: anchor (#7343)
Jealh-h Dec 16, 2024
d3cde8f
Fix "won't break between majors" (#7357)
smikitky Dec 16, 2024
37e1ce9
Add React Summit 2025 to conferences.md; move past conferences to the…
denis-urban Dec 16, 2024
4b5ce91
Add missing react-error-boundary dependency (#7353)
acusti Dec 16, 2024
0805613
fix broken link in use server (#7351)
simon300000 Dec 16, 2024
a4b6074
fix: page does not exist (#7348)
Dec 16, 2024
807e7fa
Mention that uncontrolled form elements are reset by form actions (#7…
jeremy-deutsch Dec 16, 2024
0f8bd80
Code example mistake in useMemo page (#7335)
JZZICK Dec 16, 2024
3558095
docs: include React 19 blog post in sidebarBlog.json (#7331)
slorber Dec 16, 2024
93a03f1
docs: Fix broken link to next.js Suspense docs (#7318)
ethshea Dec 16, 2024
204b3f1
Remove canary flag from useFormStatus (#7349)
smikitky Dec 16, 2024
8ac5531
Rename remaining "Server Actions" (#7352)
smikitky Dec 16, 2024
9985199
docs[compiler]: clarify React DevTools support for Compiler Badges in…
piotrski Dec 16, 2024
c37fdd3
Remove forwardref from useImperativeHandle docs (#7360)
SebassNoob Dec 16, 2024
e2bba41
docs: fix readme node version (#7316)
devwqc Dec 16, 2024
8a5526e
Update lauren (#7373)
poteto Dec 17, 2024
03e74dd
Add info about App.js 2025 conference (#7154)
piaskowyk Dec 18, 2024
8e41e32
Inline stylesheets are not Suspensey yet (#7378)
eps1lon Dec 19, 2024
1517494
refactor: remove unused useTransition import in App.js and update doc…
Rekl0w Dec 19, 2024
6ae99dd
Remove forwardRef references from useRef and Manipulating the DOM wit…
mattcarrollcode Dec 20, 2024
b1a249d
Fix React Native DevTools link (#7386)
rammba Dec 24, 2024
5e3e400
bot to notify for PRs (#7408)
rickhanlonii Jan 7, 2025
c0b6baf
Fix broken Next.js base path configuration link (#7419)
amarachiugwu Jan 9, 2025
7b4f948
fix: change overflow-x-scroll to overflow-x-auto in TerminalBlock com…
clicktodev Jan 9, 2025
9c4bc28
Update server-functions.md (#7396)
Azzyxec Jan 9, 2025
9000e6e
Add React Universe Conf 2025 to list of conferences (#7421)
barbaramarkiewicz Jan 9, 2025
3b27d4f
merging all conflicts
react-translations-bot Jan 13, 2025
d472bd9
Merge branch 'sync-9000e6e0' into main-sync
bilalkarakollu Jan 18, 2025
6c0d504
Merge pull request #599 from reactjs/main-sync
anilcanboga Jan 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove forwardref from useImperativeHandle docs (#7360)
* fix: remove forwardref from useImperativeHandle docs

* Make changes more focused

* Update useImperativeHandle.md

---------

Co-authored-by: dan <dan.abramov@gmail.com>
  • Loading branch information
SebassNoob and gaearon authored Dec 16, 2024
commit c37fdd38ff6c9ef8fdb3ab198168c8f89fa458d7
68 changes: 36 additions & 32 deletions src/content/reference/react/useImperativeHandle.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ useImperativeHandle(ref, createHandle, dependencies?)
Call `useImperativeHandle` at the top level of your component to customize the ref handle it exposes:

```js
import { forwardRef, useImperativeHandle } from 'react';
import { useImperativeHandle } from 'react';

const MyInput = forwardRef(function MyInput(props, ref) {
function MyInput({ ref }) {
useImperativeHandle(ref, () => {
return {
// ... your methods ...
Expand All @@ -38,12 +38,18 @@ const MyInput = forwardRef(function MyInput(props, ref) {

#### Parameters {/*parameters*/}

* `ref`: The `ref` you received as the second argument from the [`forwardRef` render function.](/reference/react/forwardRef#render-function)
* `ref`: The `ref` you received as a prop to the `MyInput` component.

* `createHandle`: A function that takes no arguments and returns the ref handle you want to expose. That ref handle can have any type. Usually, you will return an object with the methods you want to expose.

* **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.

<Note>

Starting with React 19, [`ref` is available a prop.](/blog/2024/12/05/react-19#ref-as-a-prop) In React 18 and earlier, it was necessary to get the `ref` from [`forwardRef`.](/reference/react/forwardRef)

</Note>

#### Returns {/*returns*/}

`useImperativeHandle` returns `undefined`.
Expand All @@ -54,40 +60,38 @@ const MyInput = forwardRef(function MyInput(props, ref) {

### Exposing a custom ref handle to the parent component {/*exposing-a-custom-ref-handle-to-the-parent-component*/}

By default, components don't expose their DOM nodes to parent components. For example, if you want the parent component of `MyInput` to [have access](/learn/manipulating-the-dom-with-refs) to the `<input>` DOM node, you have to opt in with [`forwardRef`:](/reference/react/forwardRef)

```js {4}
import { forwardRef } from 'react';
To expose a DOM node to the parent element, pass in the `ref` prop to the node.

const MyInput = forwardRef(function MyInput(props, ref) {
return <input {...props} ref={ref} />;
});
```js {2}
function MyInput({ ref }) {
return <input ref={ref} />;
};
```

With the code above, [a ref to `MyInput` will receive the `<input>` DOM node.](/reference/react/forwardRef#exposing-a-dom-node-to-the-parent-component) However, you can expose a custom value instead. To customize the exposed handle, call `useImperativeHandle` at the top level of your component:
With the code above, [a ref to `MyInput` will receive the `<input>` DOM node.](/learn/manipulating-the-dom-with-refs) However, you can expose a custom value instead. To customize the exposed handle, call `useImperativeHandle` at the top level of your component:

```js {4-8}
import { forwardRef, useImperativeHandle } from 'react';
import { useImperativeHandle } from 'react';

const MyInput = forwardRef(function MyInput(props, ref) {
function MyInput({ ref }) {
useImperativeHandle(ref, () => {
return {
// ... your methods ...
};
}, []);

return <input {...props} />;
});
return <input />;
};
```

Note that in the code above, the `ref` is no longer forwarded to the `<input>`.
Note that in the code above, the `ref` is no longer passed to the `<input>`.

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:

```js {7-14}
import { forwardRef, useRef, useImperativeHandle } from 'react';
import { useRef, useImperativeHandle } from 'react';

const MyInput = forwardRef(function MyInput(props, ref) {
function MyInput({ ref }) {
const inputRef = useRef(null);

useImperativeHandle(ref, () => {
Expand All @@ -101,8 +105,8 @@ const MyInput = forwardRef(function MyInput(props, ref) {
};
}, []);

return <input {...props} ref={inputRef} />;
});
return <input ref={inputRef} />;
};
```

Now, if the parent component gets a ref to `MyInput`, it will be able to call the `focus` and `scrollIntoView` methods on it. However, it will not have full access to the underlying `<input>` DOM node.
Expand Down Expand Up @@ -134,9 +138,9 @@ export default function Form() {
```

```js src/MyInput.js
import { forwardRef, useRef, useImperativeHandle } from 'react';
import { useRef, useImperativeHandle } from 'react';

const MyInput = forwardRef(function MyInput(props, ref) {
function MyInput({ ref, ...props }) {
const inputRef = useRef(null);

useImperativeHandle(ref, () => {
Expand All @@ -151,7 +155,7 @@ const MyInput = forwardRef(function MyInput(props, ref) {
}, []);

return <input {...props} ref={inputRef} />;
});
};

export default MyInput;
```
Expand Down Expand Up @@ -195,11 +199,11 @@ export default function Page() {
```

```js src/Post.js
import { forwardRef, useRef, useImperativeHandle } from 'react';
import { useRef, useImperativeHandle } from 'react';
import CommentList from './CommentList.js';
import AddComment from './AddComment.js';

const Post = forwardRef((props, ref) => {
function Post({ ref }) {
const commentsRef = useRef(null);
const addCommentRef = useRef(null);

Expand All @@ -221,16 +225,16 @@ const Post = forwardRef((props, ref) => {
<AddComment ref={addCommentRef} />
</>
);
});
};

export default Post;
```


```js src/CommentList.js
import { forwardRef, useRef, useImperativeHandle } from 'react';
import { useRef, useImperativeHandle } from 'react';

const CommentList = forwardRef(function CommentList(props, ref) {
function CommentList({ ref }) {
const divRef = useRef(null);

useImperativeHandle(ref, () => {
Expand All @@ -252,17 +256,17 @@ const CommentList = forwardRef(function CommentList(props, ref) {
{comments}
</div>
);
});
}

export default CommentList;
```

```js src/AddComment.js
import { forwardRef, useRef, useImperativeHandle } from 'react';
import { useRef, useImperativeHandle } from 'react';

const AddComment = forwardRef(function AddComment(props, ref) {
function AddComment({ ref }) {
return <input placeholder="Add comment..." ref={ref} />;
});
}

export default AddComment;
```
Expand Down