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