将React-Navigation v5与Redux和Firebase认证结合使用

问题描述 投票:1回答:1

我正在尝试使用React-Navigation版本5(而不是Ver 4)在用户登录时从身份验证屏幕/堆栈切换到非身份验证屏幕/堆栈。文档。

但是,文档未提供使用ReduxGoogle Firebase身份验证的详细信息。成功登录后,我收到以下警告消息。

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

我认为此警告消息的原因是,对Redux操作(操作创建者)的调用导致重新呈现身份验证屏幕,而导航代码已经卸载了该屏幕! 卸载身份验证屏幕后,如何更新Redux的商店?

导航代码:

...
// If user is logged in -> display the app/main stack
if (props.loggedIn) 
{
    const MyTabs = createBottomTabNavigator();
    return (
        <NavigationContainer>
            <MyTabs.Navigator initialRouteName="UserTypeScreen">
                <MyTabs.Screen name="UserTypeScreen" component={UserTypeScreen}  />
                <MyTabs.Screen name="PermissionsScreen" component={PermissionsScreen} />
            </MyTabs.Navigator>
        </NavigationContainer>
    ); 
}
// If user is NOT logged in -> display the auth stack
else {
    const AuthStack = createStackNavigator();
    return (
        <NavigationContainer>
            <AuthStack.Navigator>
                <AuthStack.Screen name="AuthScreen" component={AuthScreen} />
                <AuthStack.Screen name="AuthLinkScreen" component={AuthLinkScreen} />
            </AuthStack.Navigator>
        </NavigationContainer>
    ); 
}
...

认证画面:

...
useEffect( () => {
    const unSubscribe = firebase.auth().onAuthStateChanged( (user) => {
        if (user) {
            const idToken = firebase.auth().currentUser.getIdToken(); 
            const credential = firebase.auth.PhoneAuthProvider.credential(idToken); 

            // The following statement causes a warning message, because it causes re-rendering of an unmounted screen
            props.acLoginUserSuccess(user, credential); 
        } 
    }); 

    return () => {
        unSubscribe();
    };
}, []); 
...

Redux Action Creator:

...
export const acLoginUserSuccess = (user, credential) => {
    return {
        type: LOGIN_USER_SUCCESS,
        payload: { user: user, credential: credential }
  };
};
...
react-native redux firebase-authentication react-navigation react-navigation-v5
1个回答
0
投票

我很快通过了您的代码,但这就是我的想法(可能是错误的。)>

文档建议在已安装的屏幕上获取令牌(示例中为App.js)。

© www.soinside.com 2019 - 2024. All rights reserved.