reactjs - 当组件离开视图时如何使用异步等待
问题描述
如何使用异步等待?
当调用 this.props.onChangeStep1() 时,渲染视图的组件被另一个组件替换。
如果组件已被交换,Async Await 是否仍然有效?
s3.upload(params, function(err, data) {
this.props.onChangeStep1(); //<----- After s3.upload I want to call this first
this.setState( //<----But I want this to run in the background
// even though the component is not in view
{
myState: "data-from-s3Upload"
});
}.bind(this)
);
解决方案
如果回调设置为稍后执行,并且启动异步调用的组件已卸载,则无论如何都会执行回调。如果回调尝试对未安装的组件进行操作(例如更改其状态),它将被视为内存泄漏,React 将在控制台中通过错误消息通知您。
推荐阅读
- python - 使用 OAuth 的 Python Twitter API 不提供电子邮件
- git - 如何在 git 上确认并一一添加所有更改
- umbraco - Umbraco 扩展标签
- typescript - 为具有条件类型的可选参数声明依赖参数类型
- optaplanner - 如何在 Optaplanner 中实现自定义终止
- html - BEM 是否会强制您创建不必要的元素?
- mongodb - 如何查找 Mongoose Array 元素的 schemaType
- mysql - SparkSQL 连接问题
- python - 如何向 Django ModelForm 添加额外的字段?
- python - 有序字典的深拷贝是否有望保留其顺序?