Skip to content

Documentation: Clarifying described behavior for ref callbacks #7839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
8 changes: 7 additions & 1 deletion src/content/reference/react-dom/components/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,13 @@ Instead of a ref object (like the one returned by [`useRef`](/reference/react/us

When the `<div>` DOM node is added to the screen, React will call your `ref` callback with the DOM `node` as the argument. When that `<div>` DOM node is removed, React will call your the cleanup function returned from the callback.

React will also call your `ref` callback whenever you pass a *different* `ref` callback. In the above example, `(node) => { ... }` is a different function on every render. When your component re-renders, the *previous* function will be called with `null` as the argument, and the *next* function will be called with the DOM node.
React will also call your `ref` callback whenever you pass a *different* `ref` callback. In the above example, `(node) => { ... }` is a different function on every render. When your component re-renders, React will call the cleanup function returned by the *previous* callback (if any), and then call the *next* function with the DOM node.

<Note>

If your ref callback doesn't return a cleanup function, React will call the previous callback with `null` as the argument for legacy compatibility.

</Note>

#### Parameters {/*ref-callback-parameters*/}

Expand Down