I have this function in a react-native (typescript) app :
import { LoginProps } from '../types';
export const signOut = async (auth: Auth, nav: LoginProps['navigation'], setUser: any) => {
try {
await auth.signOut();
nav.navigate('Login');
setUser(null);
} catch (error) {
console.log(error);
}
}
This function works as intended but if I comment or delete this line : await auth.signOut();
,
I got this error : The action 'NAVIGATE' with payload {"name":"Login"} was not handled by any navigator.
Same thing if I try to just navigate without doing anything else in the function.
I don’t understand how these two lines are linked and why the first is necessary for the second to works…
I encountered this problem while testing things to resolve this post.
Here are my types :
export type RootStackParamList = {
Home: undefined;
Login: undefined;
Register: undefined;
Game: { roomId: string | null };
}
export type NavigationProps<T extends keyof RootStackParamList> = {
navigation: StackNavigationProp<RootStackParamList, T>;
}
export type HomeProps = NavigationProps<'Home'>;
export type LoginProps = NavigationProps<'Login'>;
export type RegisterProps = NavigationProps<'Register'>;
export type GameProps = NavigationProps<'Game'>;
2
Answers
It was because my screens are defined like this :
As you can see, the variable
user
has to be undefined for the Login screen to be defined, so if the User is connected, the Login screen stays undefined in the context, and is therefor, not handled by any navigator.EDIT :
I found the answer myself like 5 minutes after posting but could not auto-answer because it was too soon.
Unfortunately I forgot to get back online later to post it... But I received a good response from @famfamfam :)
please take a look here : https://reactnavigation.org/docs/auth-flow
I think only this line work to navigate to Login
Not this line:
Because if auth = null, the
Navigator
automatically chooseLoginScreen
. So the codenav.navigate('Login');
were redundant, you dont need it.That why’s you can not navigate to
Login
by comment this lineawait auth.signOut();