Comments (3)
click() {
for (let i = 0; i < 10; i++) {
this.setState({
count: ++this.state.count,
});
}
this.state.count = 13;
console.log(this.state.count);
}
我在你的列子上这样调用,UI上面显示是13,而不是this.setState之后的值。
from blog.
@yxxuweb 针对你反馈的这个测试用例,在 react 和 preact 分别做了实验:
class App extends Component {
constructor(props) {
super(props)
this.state = {
count: 0
}
this.click = this.click.bind(this)
}
click() {
for (let i = 0; i < 10; i++) {
this.setState({
count: ++this.state.count
})
}
this.state.count = 13;
}
render() {
return (
<div>
<button onClick={this.click}>增加</button>
<div>{this.state.count}</div>
</div>
)
}
}
在 React 中(点击按钮):
- UI 上显示为 10。
- 还有一点是调用完 this.state.count = 13 后,应该调用 this.forceUpdate(),UI 才会生效。
在 cpreact 上表现与 preact 相同
- UI 上显示为 13。
这个案例在 cpreact 执行流程为:click 事件 => setState 函数 10 次异步调用合并 => this.state.count = 13 => render
。因此界面上显示为 13。
但是这么做确实有些违规 react 的法则,直接调用 this.state.count(没有 forceUpdate) 就改变了状态。
react 中的执行流程应该为:click 事件 => setState 函数 10 次异步调用合并 => render => this.state.count = 13
即 react 中 this.state.count 应该放在 render 函数的后面再执行,有点好奇 react 事件循环机制是如何实现的。如果有好的想法欢迎指正。
from blog.
@MuYunyun 在react中循环10次调用setState
,UI显示上还是10。这一点不是很理解,我也很好奇react 事件循环机制是如何实现的,大佬能写篇文章解释一下吗?
from blog.
Related Issues (20)
- 设计模式--单例篇 Error: Missing initializer in const declaration HOT 4
- http://muyunyun.cn/blog/ 404了 HOT 5
- http://muyunyun.cn/blog/ 又404了 HOT 2
- 拼多多内推 HOT 1
- 提供个人博客的 RSS 资源 HOT 1
- TypeScript extends 精读与实践 HOT 14
- 【实践】树莓派使用 HOT 2
- 基于树莓派部署 code-server HOT 1
- React Conf 2021 HOT 3
- 2021-d2-conf
- 提供文章推荐页 HOT 1
- 搭建树莓派网盘 HOT 1
- 添加我阅读过的书/书籍推荐章节 HOT 2
- 【开源自荐】推荐一个每日更新的前端面试题库 HOT 1
- Todo: 在 React 使用 Rust 编写的函数 HOT 2
- 计算机系统要素-从零开始构建现代计算机 HOT 1
- 首页增加最近新增的前 10 篇文章列表 HOT 1
- husky 未生效解决方案 HOT 2
- 简版 express.js 的实现 链接失效 HOT 2
- SEO 在 SPA 站点中的实践 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.