react-native - 无论页面如何,都会调用组件内部的 setTimeout
问题描述
我有一个 setTimeout 调用,每 5 分钟调用一次。问题是,无论我在哪个页面上,超时仍然会被调用并且正在进行 api 调用。
我的 setTimeout 组件位于主页内,理想的做法是在不再位于主页上时仅杀死该组件。我想避免破坏主页组件而不必重新获取数据。
我的 setTimeout 代码是这样的:
sendCords(){
this.props.actions.findCords()
this.beginTimeout()
}
beginTimeout(){
let timer = setTimeout(this.sendCords,300000);
this.setState({timer});
}
注意:生命周期钩子 EG componentWillUnMount 没有被调用,因为页面就像一个堆栈 - 并且页面没有卸载。我正在使用 react-native-router-flux
任何帮助将非常感激。
解决方案
好吧,我最终不得不做一些事情,在我的操作中,我会检查我正在调用的页面,如果没有在主页上调用它,则发送一个空操作。这将触发清除超时。需要对我的组件进行强制刷新
<Comp refresh={() => this.setState({val:!this.state.val} />
然后我会在导航回主页并需要再次设置超时时使用 shouldComponentUpdate :
shouldComponentUpdate(nextProps, nextState){
if(this.props.removeTimer && !nextState.removeTimer){
this.timeOutFunc()
}
推荐阅读
- reactjs - 如何在没有状态值更新的情况下停止 React 重新渲染?
- javascript - Devextreme DxDropDownButton 在我重新加载应用程序的网页后消失
- java - Kafka 自定义反序列化器
- python-3.x - 我正在尝试使用 python 列出/复制具有模式的文件,例如 name_123.jpg 从 1 个文件夹到其他文件夹
- image-processing - tf2.0如何使用DC GAN去除图像水印?
- selenium - Selenium 文件上传在 Azure Pipeline/VM 机器中不起作用
- php - 将 enctype="multipart/form-data 添加到我的表单并尝试提交后,它转到另一个页面
- typescript - Typescript 中的通用基础对象
- amazon-web-services - 具有 Nginx 配置的 AWS Ec2 实例为 www 获得 405
- php - ZOHO 项目文档 API 返回空响应,状态码为 201(已创建)