错误边界应实现getDerivedStateFromError()

kor*_*irl 6 javascript firebase react-native firebase-authentication

我当时在一个项目上工作,并且一切正常,突然由于这个错误我的应用程序崩溃了:

Warning: %s: Error boundaries should implement getDerivedStateFromError(). 
In that method, return a state update to display an error message or fallback UI., RootErrorBoundary
Run Code Online (Sandbox Code Playgroud)

错误在以下文件中:

* src/config/routes.js:31:27 in <unknown>
* src/modules/auth/actions.js:69:29 in <unknown>
Run Code Online (Sandbox Code Playgroud)

在包含route.js的_this.setState的行中:

componentDidMount() {
    let _this = this;
    store.dispatch(checkLoginStatus((isLoggedIn) => {
        _this.setState({isReady: true, isLoggedIn});
    }));
}
Run Code Online (Sandbox Code Playgroud)

并在包含actions.js的callback(isLoggedIn)的行中:

export function checkLoginStatus(callback) {
    return (dispatch) => {
        auth.onAuthStateChanged((user) => {
            let isLoggedIn = (user !== null);

            if (isLoggedIn) {
                //get the user object from the Async storage
                AsyncStorage.getItem('user', (err, user) => {
                    if (user === null) isLoggedIn = false //set the loggedIn value to false
                    else dispatch({type: t.LOGGED_IN, data: JSON.parse(user)})

                    callback(isLoggedIn);
                });
            } else {
                dispatch({type: t.LOGGED_OUT});
                callback(isLoggedIn);
            }
        });
    };
}
Run Code Online (Sandbox Code Playgroud)

请帮助我解决此问题。

小智 -8

我通过清除应用数据解决了这个问题

  • 您能详细说明一下您具体遇到了什么问题以及解决问题的步骤吗?也许对于那些正在寻找解决方案但不熟悉设置并且可能对清除应用程序数据的含义感到困惑的人? (8认同)