Reactjs 这在React组件的异步函数中不起作用
在我的React组件中,未执行this.login。我相信这是因为等待函数改变了上下文,但我不确定如何修复它Reactjs 这在React组件的异步函数中不起作用,reactjs,Reactjs,在我的React组件中,未执行this.login。我相信这是因为等待函数改变了上下文,但我不确定如何修复它 await this.props .SignupUser({ variables: { email: this.state.email, password: this.state.password,
await this.props
.SignupUser({
variables: {
email: this.state.email,
password: this.state.password,
name: this.state.name,
},
})
.then(() => {
this.login();
})
.catch(error => {
this.setState({ wait: false });
const errorMessage = error.graphQLErrors[0].functionError;
this.setState({ error: errorMessage });
});
删除wait关键字,它应该可以工作
或者,我们需要以不同的方式实施
函数必须在下面代码的函数声明中添加async关键字
await this.props
.SignupUser({
variables: {
email: this.state.email,
password: this.state.password,
name: this.state.name,
},
})
.then(() => {
this.login();
})
.catch(error => {
this.setState({ wait: false });
const errorMessage = error.graphQLErrors[0].functionError;
this.setState({ error: errorMessage });
});
或
API调用中是否有错误。是否签入了catch
try {
const user = await this.props
.SignupUser({
variables: {
email: this.state.email,
password: this.state.password,
name: this.state.name,
},
});
this.login();
} catch (e) {
this.setState({ wait: false });
const errorMessage = error.graphQLErrors[0].functionError;
this.setState({ error: errorMessage });
}