Skip to content

Clarify ref callback cleanup behavior in common ref docs - #8579

Open
smg99 wants to merge 1 commit into
reactjs:mainfrom
smg99:docs/clarify-ref-callback-cleanup
Open

smg99 wants to merge 1 commit into
reactjs:mainfrom
smg99:docs/clarify-ref-callback-cleanup

Conversation

@smg99

@smg99 smg99 commented Aug 5, 2026

Copy link
Copy Markdown

Fixes #7811

Summary

  • Clarify the ref callback behavior when callbacks change between renders
  • Explicitly describe the cleanup-function path as the primary behavior
  • Clarify that passing null to the old callback is legacy fallback behavior when no cleanup is returned

Why

Issue #7811 points out ambiguity in the current wording, especially around when React calls a previous callback with null. This change aligns wording with current React 19 cleanup semantics while preserving backwards-compatibility notes.

Validation

  • Ran yarn lint
  • Ran yarn lint-heading-ids

@smg99

smg99 commented Aug 5, 2026

Copy link
Copy Markdown
Author

Requesting review for this wording clarification.

This updates the ref callback section to emphasize the cleanup-function path first, and clarifies that null is the legacy fallback when no cleanup function is returned.

Happy to refine wording further if maintainers prefer a different phrasing.

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

Size changes

Details

📦 Next.js Bundle Analysis for react-dev

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

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 the cleanup function returned from your 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 calls the previous callback's cleanup function (if provided). If no cleanup function is returned, React calls the previous callback with `null`. Then React calls the next callback with the DOM node.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we preserve the existing wording and emphasis while clarifying the cleanup behavior?

Suggested change
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 calls the previous callback's cleanup function (if provided). If no cleanup function is returned, React calls the previous callback with `null`. Then React calls the next callback 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, the cleanup function returned by the *previous* function will be called, and the *next* function will be called with the DOM node. If no cleanup function is returned, the *previous* function will be called with `null` as the argument instead.

[See an example of using the `ref` callback.](/learn/manipulating-the-dom-with-refs#how-to-manage-a-list-of-refs-using-a-ref-callback)

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.
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 the cleanup function returned from your callback.

@aurorascharff aurorascharff Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here try to preserve whats already written as it doesnt seem important for the intended clarification, meaning keeping "the callback"

This branch was successfully deployed

1 active deployment
Preview — 90b9f6e2 Deployed Aug 5, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Suggestion]: clarify behaviour of ref callbacks

2 participants