设置状态未在React中设置

时间:2020-05-19 13:54:41

标签: reactjs

我有以下代码

  this.setState({loading: true}, () => {
            if (googleUser && googleUser.profileObj) {
                app.auth().signOut();
                // We need to register an Observer on Firebase Auth to make sure auth is initialized.
                var unsubscribe = app.auth().onAuthStateChanged(firebaseUser => {
                    unsubscribe();
                    // Check if we are already signed-in Firebase with the correct user.
                    if (!this.isUserEqual(googleUser, firebaseUser)) {
                        // Build Firebase credential with the Google ID token.
                        var credential = firebase.auth.GoogleAuthProvider.credential(
                            googleUser.getAuthResponse().id_token);
                        // Sign in with credential from the Google user.
                        app.auth().signInWithCredential(credential).catch(error => {
                            // Handle Errors here.
                            var errorCode = error.code;
                            var errorMessage = error.message;
                            // The email of the user's account used.
                            var email = error.email;
                            // The firebase.auth.AuthCredential type that was used.
                            var credential = error.credential;
                            this.setState({error: errorMessage, loading: false})
                        }).then(() => {
                            app.auth().onAuthStateChanged((user) => {
                                if (user) {
                                    user.getIdToken().then(idToken => {
                                        // Session login endpoint is queried and the session cookie is set.
                                        // CSRF protection should be taken into account.
                                        const csrfToken = Utils.getCookie('csrfToken');
                                        this.postIdTokenToSessionLogin('/auth/session-login', idToken, csrfToken, 'google', googleUser.profileObj.googleId);
                                    });
                                } else {
                                    this.setState({error: "There was an error", loading: false});
                                }
                            });
                        })
                    } else {
                        this.setState({error: "You are already signed in.", loading: false});
                    }
                });
            }
        });

代码无法正常工作,在调用setState中的回调函数之前,未将load设置为true且未重新呈现。

有人知道发生了什么吗?

0 个答案:

没有答案