reactjs / reactjs/react.dev

[Suggestion]: clarify behaviour of ref callbacks

未關閉 適合新手
#7,811 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

type: documentation
主要語言
JavaScript
星號
11.8k
分支
7.9k
平均合併
1 天 11 小時
30 天內合併 PR
11

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從連結的 react.dev 頁面中的 ref-callback 部分開始,並檢視精簡後的 StackBlitz 測試案例,以驗證所描述的行為。釐清醒目標示的句子,使其在回呼傳回清理函式時仍然準確,並確保這段措辭不依賴後面的註記也能說得通。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
javascript, react
領域
documentation
Issue 類型
文件
難度
2/5
預估耗時
1-3 小時
活躍度
冷清
描述清晰度
基本清楚
新手友好度
72/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。