我是靠谱客的博主 时尚御姐,这篇文章主要介绍【解决】React setState延迟delay导致数据更新不及时,代码无法正确执行,现在分享给大家,希望可以做个参考。

之前React setState后在另一个函数调用state的值没有更新:

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// init state.type = 'new'; activeMenu(type) { // type = top if (type == this.state.type) return; this.loadList(type); } loadList(type) { this.setState({ items: [], type: type }); ......... // type is top, but this.state.type still is new }

 

 

改进后的setState是这样的:

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 使用setState的回调函数即可解决 activeMenu(type) { if (type == this.state.type) return; this.setState({ items: [], page: 1, type: type, isLoading: false, isLoadedFinished: false }, function() { this.loadList(); }); } loadList() { // 这样我们就可以愉快及时的使用 state里面的值 。。。 。。。 }

 

 

 

有疑问或技术交流,扫描公众号一起讨论学习。

更多React在线学习访问:http://each.sinaapp.com/react/index.html

最后

以上就是时尚御姐最近收集整理的关于【解决】React setState延迟delay导致数据更新不及时,代码无法正确执行的全部内容,更多相关【解决】React内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部