reactjs / reactjs/react.dev

[Suggestion]: clarify behaviour of ref callbacks

Open Beginner friendly
#7,811 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: documentation
Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

Summary

The described behaviour for ref callbacks with null as an argument is ambiguous.

Page

https://react.dev/reference/react-dom/components/common#ref-callback

Details

From https://react.dev/reference/react-dom/components/common#ref-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.

If I understand correctly, the emphasised sentenced is not correct when the ref callback returns a clean up function, as is the case in the referenced example. This needs to be clarified.

There is a note later on which helps a bit, but I still think this sentence could be made clearer so it makes sense in isolation.

Reduced test case to confirm this behaviour: https://stackblitz.com/edit/vitejs-vite-2r9rn6m2?file=src%2FApp.tsx&terminal=dev

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ref-callback section at the linked react.dev page and review the reduced StackBlitz test case to verify the behavior described. Clarify the highlighted sentence so it remains accurate when the callback returns a cleanup function, and ensure the wording makes sense without relying on the later note.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.