reactjs - Waiting for request to finish before performing operation ReactJS
问题描述
I have an axios call inside my react application
}).then(res =>
this.setState({
name: res.data.fname,
sName: res.data.sname,
percentage: res.data.percentage,
result: res.data.result,
showResult: true,
type: this.typeHandler(res.data.percentage)
}));
}
after the request is done AND the state is set, I want to set input forms to blank. So i tried this
}).then(res =>
this.setState({
name: res.data.fname,
sName: res.data.sname,
percentage: res.data.percentage,
result: res.data.result,
showResult: true,
type: this.typeHandler(res.data.percentage)
})
).then(
this.setState(prevState =>{
return {inputName: '', inputSname: ''}
}))
}
using another promise, being resolved after the first, but the forms are still set to blank before the request state is being set. I have tried using a callback as well, but the first .then() method, does not accept a second argument. How can I solve this?
解决方案
setState 可以得到回调
setState(updater[, callback])
而不是另一个然后将此回调传递给 setState
推荐阅读
- javascript - 如何修复 React Native Agora 异步错误
- c - 如何开发包含 PostGIS 库的 PostgreSQL 扩展
- ios - _dispatch_assert_queue_fail 在 iOS 14 上崩溃
- java - 如何将 .equals 答案(对或错)更改为(是或否)?
- proxy - {"message":"没有与这些值匹配的路由"}
- python - **AttributeError:“NoneType”错误。BS的问题
- mysql - 没有用户在线时MySql显示800个连接?
- php - 如何配置 vscode 实时服务器以正确处理 php 文件(与 .html 文件配合得很好)?
- python - Docker 容器中的无效进程
- android - 无论是否使用 Android Management API,我如何在 Google Play 上发布私有应用程序?