How to manage stack navigation based on app module? - react-native

I am new in React Native and I want to create stack navigation based on modules like
Auth
Login
Register
Forgot
Home
Home
Dashboard
Tabs
Profile
1 - Auth stack have only above routes access where user can only navigate
2 - Home stack have only above routes access where user can navigate around home stack
Please guide here or any sample existing

This is explained with examples in react navigation authentication flows guide.
Here's simple example based on your routes -
isSignedIn ? (
<>
<Stack.Screen name="Dashboard" component={DashboardScreen} />
<Stack.Screen name="Tabs" component={TabsScreen} />
<Stack.Screen name="Profile" component={ProfileScreen} />
</>
) : (
<>
<Stack.Screen name="Login" component={LoginScreen} />
<Stack.Screen name="Register" component={RegisterScreen} />
<Stack.Screen name="Forgot" component={ForgotScreen} />
<Stack.Screen name="Home" component={HomeScreen} />
</>
);

Related

How to navigate to a screen which is not on the <Tab.Navigator> in React-Nativ?

I have a <Tab.Navigator> and it has four <Tab.Screen> elements. What i try to do is, to press a button inside a specific <Tab.Screen> and open an another screen on top of it. But i don't want this another screen to have a <Tab.Screen> navigator in the <Tab.Navigator> bar.
I thought maybe there's an option to hide, make invisible a <Tab.Screen> but i couldn't find any documentation about it.
Is it possible to achieve this ?
According the official doc. You can reorganize your navigation and put the bottom tabs navigator inside the stack navigator like this
function HomeTabs() {
return (
<Tab.Navigator> // Here you can also navigate to both Profile and Settings
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Feed" component={Feed} />
<Tab.Screen name="Notifications" component={Notifications} />
</Tab.Navigator>
);
}
function App() {
return (
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeTabs} />
<Stack.Screen name="Profile" component={Profile} /> // Here you won't have any tabs
<Stack.Screen name="Settings" component={Settings} /> // Here neither
</Stack.Navigator>
);
}
You have to nest navigators with the stack navigator as the outer navigator and the tab navigator as the inner navigator:
https://reactnavigation.org/docs/nesting-navigators
You have to reorganize your navigation structure , as documentation describe https://reactnavigation.org/docs/hiding-tabbar-in-screens
Let's say we have 5 screens: Home, Feed, Notifications, Profile and Settings, and your navigation structure looks like this:
function HomeStack() {
return (
<Stack.Navigator>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Profile" component={Profile} />
<Stack.Screen name="Settings" component={Settings} />
</Stack.Navigator>
);
}
function App() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeStack} />
<Tab.Screen name="Feed" component={Feed} />
<Tab.Screen name="Notifications" component={Notifications} />
</Tab.Navigator>
);
}
With this structure, when we navigate to the Profile or Settings screen, the tab bar will still stay visible over those screens.
But if we want to show the tab bar only on the Home, Feed and Notifications screens, but not on the Profile and Settings screens, we'll need to change the navigation structure. The easiest way to achieve this is to nest the tab navigator inside the first screen of the stack instead of nesting stack inside tab navigator:
function HomeTabs() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Feed" component={Feed} />
<Tab.Screen name="Notifications" component={Notifications} />
</Tab.Navigator>
);
}
function App() {
return (
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeTabs} />
<Stack.Screen name="Profile" component={Profile} />
<Stack.Screen name="Settings" component={Settings} />
</Stack.Navigator>
);
}
After re-organizing the navigation structure, now if we navigate to the Profile or Settings screens, the tab bar won't be visible over the screen anymore.

Structure for nested navigation with react-native-navigation

I am currently building my first app using react-native and i am struggeling with the right structure for nested navigation using react-native-navigation .
My app should be navigatable using a Tab-bar. On every screen of that Tab-bar I might need to have a Stack navigation available.
App.js:
<NavigatonContainer>
<BottomTab.Navigator>
<BottomTab.Screen name="Screen1" component={Screen1}/>
<BottomTab.Screen name="Screen2" component={Screen2}/>
<BottomTab.Screen name="Screen3" component={Screen3}/>
<BottomTab.Navigator/>
<NavigationContainer/>
Now if i want to use a Stack navigation inside Screen1, what would be the best approach? My approach would be to move all content that should be display when the first Tab is active to a new screen called "Screen1Start" and then having following code on "Screen1".
Screen1.js:
<Stack.Navigator>
<Stack.Screen name="Screen1Start" component={Screen1Start}/>
<Stack.Screen name="Screen1Settings" component={Screen1Settings}/>
<Stack.Screen name="Screen1Details" component={Screen1Details}/>
</Stack.Navigator>
Is that the right thing to do? Are there better approaches to this? Thanks for your help!
You can have a stack inside of a stack and then navigate to each individually with navigation.navigate('name'). In the react navigation docs you can read more about navigating inside nested navigators here.
Pretty quick example would be:
function Home() {
return (
<Tab.Navigator>
<Tab.Screen name="Feed" component={Feed} />
<Tab.Screen name="Messages" component={Messages} />
</Tab.Navigator>
);
}
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={Home}
options={{ headerShown: false }}
/>
<Stack.Screen name="Profile" component={Profile} />
<Stack.Screen name="Settings" component={Settings} />
</Stack.Navigator>
</NavigationContainer>
);
}
Here you can see that Home is a stack of feed and messages and then you have individual screens for Profile and Settings. Like so you can have another stack instead of Profile or Settings binded by another navigator such as toptab or bottomtab.
The react navigation docs explain this very detailed and easy to understand. I would recommend you to read from start to finish so you have a deeper understanding of the navigation.

How to implement Mini View like PipView in mobile app with React-Native

I want to implement a Mini View or Floating View (for WebRTC calling) in React-Native that should be persist or opened until we close it and it should be remain opened if we navigate to other screen any idea about this?
You can create a component and add it along with the navigation. It will appear on all the screens. You can absolutely position and apply animation as per your requirement.
<SafeAreaProvider>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="login"
component={Login}
options={{
headerShown: false
}} />
<Stack.Screen name="home" component={Home} />
<Stack.Screen name="screen2" component={Screen2} />
<Stack.Screen name="screen3" component={Screen3} />
</Stack.Navigator>
<IncomingCall /> // component that is floating and shared between screens
</NavigationContainer>
</SafeAreaProvider>

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

React Navigation Screen Skipped During Rendering Using `if` Statement, But Works Using Nested Ternaries

Issue: I am trying to conditionally render screens in a navigator, but it skips the first screen (GDPR), and goes to the main App screen directly even though the conditions are met.
code here:
if (!!firstLogin && !acceptedPrivacyPolicy) {
console.log('rendering....', !!firstLogin, !acceptedPrivacyPolicy)
return (
<FirstLoginStack.Navigator
headerMode={'none'}
screenOptions={{
cardStyleInterpolator:
CardStyleInterpolators.forHorizontalIOS
}}>
<FirstLoginStack.Screen name={'GDPR'} component={GDPR} />
<FirstLoginStack.Screen name={'AppTabs'} component={AppTabs} />
</FirstLoginStack.Navigator>
)
}
However, when I use a nested ternary, it works fine:
return (
<AppMainStack.Navigator
headerMode={'none'}
screenOptions={{
cardStyleInterpolator:
CardStyleInterpolators.forHorizontalIOS
}}>
{firstLogin ?
<>
{!acceptedPrivacyPolicy ? <AppMainStack.Screen name={'GDPR'} component={GDPR} /> : null}
<AppMainStack.Screen name={'AppTabs'} component={AppTabs} />
</>
: <AppMainStack.Screen name={'AppTabs'} component={AppTabs} />
}
</AppMainStack.Navigator>
)
Why is this the case?
We can define different screens based on some condition. For example, if the user is signed in, we can define Home, Profile, Settings etc. If the user is not signed in, we can define SignIn and SignUp screens.
For example:
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} />
</>
)
It's already defined workflow on React Navigation Auth-Flow