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

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/

Related

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 make a stack navigator cover entire screen using React Navigation

I am building a React Native project with Expo. For navigation, I am using react-navigation 6.0 with the following code for my main component:
export default class App extends Component {
render() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName={'SignIn'} screenOptions={{
headerShown: false
}}>
<Stack.Screen name="SignIn" component={SigninScr} />
<Stack.Screen name="SignUp" component={SignupScr} />
<Stack.Screen name="EmailVerification" component={EmailVerificationScr} />
</Stack.Navigator>
</NavigationContainer>
);
}
}
This works fine, but I haven't been able to make Stack.Navigator cover the entire screen. There is a weird area that's not covered by the Stack.Navigator:
I was reading through some of the documentation about stack navigator, but couldn't find any props or styling to force it to cover 100% of the screen. Does anyone know how to accomplish this?

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-native going back from a nested navigation container does not work

I am creating a nested navigation container in react-native and the stack looks like this:
-Main Navigation Container:
- Home
- Market Navigation Container:
- Market
- Cart
- About
When I go to home or about and go back, it works properly. However, when I go back from Market or cart (Which I expect to go to the Home page) it shows an error saying:
The action 'POP' with payload {"count":1} was not handled by any navigator.
Is there any screen to go back to?
This is a development-only warning and won't be shown in production.
This is my code for the main navigation container:
<NavigationContainer independent={true}>
<Stack.Navigator>
<Stack.Screen options={myOptions} name="Home" component={Home} />
<Stack.Screen
options={{ headerShown: false }}
name="MarketNavigation"
component={MarketNavigation}
/>
<Stack.Screen options={myOptions} name="About" component={About} />
</Stack.Navigator>
</NavigationContainer>
And this is my code for the market navigation:
<NavigationContainer ref={navigationRef} independent={true}>
<Stack.Navigator>
<Stack.Screen
options={myOptions}
name="Market"
component={Market}
/>
<Stack.Screen
options={myOptions}
name="Cart"
component={Cart}
/>
</Stack.Navigator>
</NavigationContainer>
I faced this when navigating to nested navigator using "screen" option like this:
navigation.navigate('Root', {
screen: 'Settings'
})
;
As mentioned in the documentation:
By default, when you navigate a screen in the nested navigator, the
specified screen is used as the initial screen and the initial route
prop on the navigator is ignored.
To solve this you should use "initial: false" like so:
navigation.navigate('Root', {
screen: 'Settings',
initial: false,
});
Could you add in the code where you navigate from Home to MarketNavigation and back?

React Navigation Reset on Nested Navigator

I am using React-Navigation v 5.0.1, I have a Drawer navigator nested inside Stack navigator that looks like this:
MainStack.js
function MainStack() {
return(
<Stack.Navigator>
<Stack.Screen name="Splash" component={Splash} />
<Stack.Screen name="Login" component={Login} />
<Stack.Screen name="HomeDrawerStack" component={HomeDrawerStack} />
</Stack.Navigator>
)
}
HomeDrawerStack.js
function HomeDrawerStack() {
return (
<Drawer.Navigator>
<Drawer.Screen name="Home" component={Home} />
</Drawer.Navigator>
)
}
When I tap a logout button on Home screen, how do I navigate to Login screen and reset / clear all other screens? (so it triggers the componentWillUnmount on other screens)
This code will navigate to Login Screen after clearing the stack. so that back button or back swipe will not work to navigate to HomeDrawerStack
navigation.reset({
index: 0,
routes: [{ name: "Login" }],
});
if I share how I handled it, it is like, I am sharing my code snippet as png here, so what I have done is appended Navigator with redux, and I use to change my stack or replace my stack by dispatching event to store attached to Navigator.