Forget Complete navigation history React Native - react-native

I am building a react native app that involves a payment process and on finishing the transaction a user is navigated to the homepage,the issues is the user can go back to the previous screen which is not what i want.
I found out this solution that works halfway
navigation.replace("MainScreen");
since even though it replaces the previous screen it goes back to another previous screen.
Is there a way I can forget the entire navigation after I finish my transaction
navigation Stack
<Stack.Navigator
initialRouteName="Home"
}}
>
<Stack.Screen
name="Home"
component={Home}
></Stack.Screen>
<Stack.Screen
name="MainScreen"
component={MainScreen}
></Stack.Screen>
<Stack.Screen
name="Cart"
component={Cart}>
</Stack.Screen>
<Stack.Screen
name="Orders"
component={Orders}
></Stack.Screen>
<Stack.Screen
name="OrderDetails"
component={OrderDetails}
></Stack.Screen>
<Stack.Screen
name="Checkout"
component={Checkout}
></Stack.Screen>
</Stack.Navigator>
i am trying to navigate from checkout to mains screen

When you finish your workflow, instead of navigating to the MainScreen,
which should be something like this:
navigation.navigate('MainScreen')
try to use this code:
navigation.reset( {
index: 0,
routes: [{name: 'MainScreen'}]
})
Please note that using this method, there might be a slight delay (depending on how much screens are presented on your navigation stack).
To avoid this lag, it might be better to remove only screens connected with the transaction, but you stated that you want to forget whole stack, so this is the method.

This worked for me.
There is a popToTop method in naviagtion 6 that takes you to the first screen of your stack, if that is not the screen you want to be you then simply navigate to the page you want to be immediately. Like so,
navigation.popToTop()
navigation.navigate('MainScreen')

Related

React Native Login to Home Screen navigation

I'm new to React Native, and want to build a login page that connects to the home page, but if I use a single stack navigator, I get the little arrow in the top lefthand corner that just allows the user to go back to the login page without clicking on the logout button, which wouldn't log them out correctly. How can I make it so that arrow goes away on the Home Screen? Do I need to make 2 stack navigators and link them together somehow? I don't have any code written yet, I'm just looking for a general explanation before I get started.
Got it, need to set "headerLeft" to null in the Stack Screen
You can also check the documentation of React Navigation authentication flow
according to the documentation Don't manually navigate when conditionally rendering screens between Auth and Home screens.
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} />
</>
);
Full documentation of React Navigation

Render one component over another at a certain route with react-navigation

I have a stack navigator with react router, using #react-navigation/native-stack. My home screen is a map, and I have a few menu components:
export const App = () => {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen
name="Home"
options={{ headerShown: false }}
component={Home}
/>
<Stack.Screen
name="SignIn"
component={SignIn}
/>
</Stack.Navigator>
</NavigationContainer>
)
}
Home is the map component, and SignIn, as well as some other components, are menu components that take over the screen when navigated to, pushing Home out of the way, as expected.
I want to be able to 'navigate' to another component at a certain route, call it Overlay. When routed here, I want to show the home screen, with an Overlay component laid over the top of part of the Home component, while still maintaining interactivity with the part of the Home screen that is not covered. However, if I do this:
<Stack.Navigator initialRouteName="Home">
<Stack.Screen
name="Home"
options={{ headerShown: false }}
component={Home}
/>
<Stack.Screen
name="Overlay"
component={Overlay}
/>
</Stack.Navigator>
Then the Home screen moves away, and is replaced by Overlay.
How can I show a component overlaid over the top of my Home screen when the user is routed to a certain route? This is something easily done with react-router, but I'm not sure if there is a parallel in react-navigation. Is react-navigation not the right approach for this? Is it better to conditionally render the Overlay component based on a state/redux variable?

How to reset React Navigation to initial default state (the state that the app opens)?

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.
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

How can we replace switch navigator of navigation 4 to navigation 5 in class based components (without using redux)?

Assume we have a splash screen where we check is user logged in or not. If logged in then navigate to shop screen else navigate to auth screen.
My question is how i will replace switch navigator of navigation 4 to navigation 5 where we have a splash screen, auth screen and shop screen? Please explain with example
Thanks you all.
Please help
The react-navigation v5 has converted its usability to Component base, and there is no such a kind of SwitchNavigator feature available.
We can use the react-navigation v5 as a component rendering to declare our screens and create our navigators.
As we are able to use the conditions in component rendering so we can put the condition in navigator rendering to check whether a user is logged in or not, if yes then show the MainApp navigation otherwise show AuthNavigation.
For Ex.
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} />
</>
)
For more information check-out the official doc:
https://reactnavigation.org/docs/auth-flow

react-navigation always mount initial screen when StackNavigator renders (v5)

In react-navigation v5, is there a way to always mount/render an initial screen to be the first screen in the stack when the StackNavigator renders?
You need to pass initialRouteName prop inside Stack.Navigator of your application.
Example Code :
<NavigationContainer>
<Stack.Navigator
initialRouteName="HomeScreen" // Add this to set initial screen
screenOptions={{
headerShown: false,
}}>
<Stack.Screen name="HomeScreen" component={HomeScreen} />
<Stack.Screen name="UploadScreen" component={UploadScreen}/>
</Stack.Navigator>
</NavigationContainer>
Refer more information
Use initialRouteName props (The name of the route to render on first load of the navigator) if you want a specific screen, otherwise the first stack screen will be rendered by default.
Doc - https://reactnavigation.org/docs/stack-navigator/