react-native - componentDidUpdate 中的更新变量失败
问题描述
我在我的视图中有一个弹出窗口,在弹出窗口中有一个倒数计时器。当倒数计时器具有 value1 时,我需要隐藏弹出窗口。这是我尝试过的。
componentDidUpdate() {
if (this.state.count === 1) {
clearInterval(this.intervalDuration);
this.setState({popupvisible:false})
}
}
当计数等于 1 时出现错误,如下所示。
Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
解决方案
如果您正在设置状态变量,那么也可以在条件中添加该变量以避免重复更新。
componentDidUpdate() {
if (this.state.count === 1) {
clearInterval(this.intervalDuration);
if(this.state.popupvisible){
this.setState({popupvisible:false})}
}
}
推荐阅读
- c - 使用头文件的共享内存数据
- android - 如何在谷歌地图中获取当前位置
- r - 如何将具有每个字符串的 ID 的标头和值的字符串转换为 R 中的数据帧
- html - 在后端更改时如何不更新显示的图像?
- php - php json_decode 将数据库 json 字符串视为对象
- javascript - 数据表抛出无效的 JSON 响应
- elasticsearch - curl: (7) 无法连接到 localhost 端口 9200:连接被拒绝。即使在配置了 /etc/elasticsearch/elasticsearch.yml
- html - 即使 x 和 y 正确,SVG 元素也会重叠
- spring-boot - 在 Spring Boot 的另一个 Controller-Service-Repository 中调用 Repository 类方法的最佳实践?
- cmd - 使用 robocopy 将 zip 文件从一个文件夹复制到另一个文件夹的语法问题