setState() callback function: potential confusion of call order
還沒有人認領這個 Issue。
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 7.9k
- 平均合併
- 1 天 11 小時
- 30 天內合併 PR
- 11
描述
This paragraph from the React.Component reference page:
setState()does not always immediately update the component. It may batch or defer the update until later. This makes readingthis.stateright after callingsetState()a potential pitfall. Instead, usecomponentDidUpdateor asetStatecallback(setState(updater, callback)), either of which are guaranteed to fire after the update has been applied.
can create the impression that instead of doing:
this.setState({ value: 5 });
console.log(this.state.value); // => not necessarily 5
you can use the callback function to safely read the state back:
this.setState({ value: 5 }, () => console.log(this.state.value)); // => "guaranteed" 5
(especially if you draw a parallel between the callback function and the updater function)
and it may actually work in some cases where there's no setState batching, but as more calls get batched in the future, these constructs may generate unintended side-effects.
Instead, we could clarify that all callback functions for a batch of setState() calls get called after the entire batch has been processed.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 issue 中連結的 React.Component 參考頁面的 setState 部分開始。檢視有關 callbacks 和 batching 的措辭,然後更新這些內容,使 callback 的執行時機以及 batch 後可用的 state 都明確無歧義;完成的標準是,範例不能讓人誤以為 callback 只會觀察它自己的個別更新。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- javascript, react
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 45/100