【发布时间】:2021-02-05 18:16:57
【问题描述】:
我有以下功能,this.setState 可以在.then 方法之外使用。
autoWork(e) {
this.setState({ showWaiting: true });
actions.autoSubmit(exampleVarOne, exampleVarTwo).then(function (results) {
dbAction.fetch().then(response => {
if (response.status === 200) {
const responseData = response.data;
this.setState({ disabledButton: true })
this.setState({ showWaiting: false });
}
}).catch(function (error) {
console.log(error);
this.setState({ showWaiting: false });
});
});
}
但是我得到以下错误
Uncaught (in promise) TypeError: Cannot read property 'setState' of 未定义
对于这行代码this.setState({ disabledButton: true })
this 在then 块内无法访问?如何更新 then 块内的状态?
【问题讨论】:
标签: reactjs