【发布时间】:2020-08-02 14:04:54
【问题描述】:
我正在尝试使用 React-Navigation 版本 5(不是版本 4)在用户登录时从身份验证屏幕/堆栈切换到非身份验证屏幕/堆栈。一个简单的例子在文档中得到了很好的证明。
但是,文档没有提供使用 Redux 和 Google 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 的 store?
导航代码:
...
// 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