1

I'm trying to reset the navigation state to default state (whatever is when the app is first opened without any actions) upon user logout.

How do I achieve that?

I've seen Reset stack after navigate to login screen yet it still specifies a route and I don't know what to put there. I've tried providing undefined yet got an error.

How can I make React Navigation to just purge everything and act as if the app opened? I'm looking for something like StackActions.reset({}) (pseudocode, doesn't work) that is empty, and trigger whatever React Navigation does on app start.

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389

1 Answers1

1

You can use conditional routes, as suggested in the documents for authentication flow

like below

isSignedIn ? (
  <>
    <Stack.Screen name="Home" component={HomeScreen} />
    <Stack.Screen name="Profile" component={ProfileScreen} />
    <Stack.Screen name="Settings" component={SettingsScreen} />
  </>
) : (
  <>
    <Stack.Screen name="SignIn" component={SignInScreen} />
    <Stack.Screen name="SignUp" component={SignUpScreen} />
  </>
);

And the isSignedIn can be handled by Context Api or redux

Mohammad
  • 792
  • 1
  • 7
  • 15
  • It's a nice idea especially if I was first designing the app, yet this isn't what I'm asking. We already have the logic in a working app, I can't change that. I just need to reset the state, regardless of how I structured the app and rendering. – Can Poyrazoğlu Apr 21 '22 at 11:40