-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
同步setState这部分有没有demo code? #8
Comments
react-setStateSync 可以参考下这里 |
我在console里面尝试这样,再往后chain一个then不能拿到最新的state,或者说是这么用的吗? |
这种方式性能不好的,文章里也说了,用 promise 的方式替代callback |
setStateSync(state){
return new Promise(resolve=>{
this.setState(state,resolve)
})
} |
这个本质还是利用了class component中setstate有callback的性质,在函数component中这个方法就不适用了,我帖子里提到的这一种,在函数component中适用吗? |
function component 里面react 提供了 useEffect 可以拿到最新的state |
对,假设不能用useeffect,有没有办法把functional component中的setstate promise化? |
在这段代码中settimeout模拟setstate的异步操作,到下一个then,还是没有办法在下一个then拿到更新后的数据。。
|
|
Promise.then setTimeout/setInterval
new Promise(resolve=>{
resolve()
}).then(()=>{
this.setState({})
})
这个好像依然无法同步获得state,在下一个.then依然拿到的依然是没更新的state
The text was updated successfully, but these errors were encountered: