Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 这在React组件的异步函数中不起作用_Reactjs - Fatal编程技术网

Reactjs 这在React组件的异步函数中不起作用

Reactjs 这在React组件的异步函数中不起作用,reactjs,Reactjs,在我的React组件中,未执行this.login。我相信这是因为等待函数改变了上下文,但我不确定如何修复它 await this.props .SignupUser({ variables: { email: this.state.email, password: this.state.password,

在我的React组件中,未执行this.login。我相信这是因为等待函数改变了上下文,但我不确定如何修复它

           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 });
}