React Native Login to Home Screen navigation - react-native

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

Related

How to go back from nested stack screen to other nested stack screen in React Navigation - React Native

I have a React Native app with React Navigation. My current navigation is two Tab navigators, and both of those have a Stack navigation with multiple screens.
My tabs look like this:
<Tab.Navigator
initialRouteName="Home"
>
<Tab.Screen
name="Home"
component={HomeView}
/>
<Tab.Screen
name="Message"
component={MessageView}
/>
</Tab.Navigator>
And my stacks are like this:
// HOME TAB
<Stack.Navigator>
<Stack.Screen name="Posts" component={HomeView} />
<Stack.Screen name="User" component={UserView} />
</Stack.Navigator>
// MESSAGE TAB
<Stack.Navigator>
<Stack.Screen name="Messages" component={MessageView}/>
<Stack.Screen name="UserProfile" component={UserProfileView} />
<Stack.Screen name="Chat" component={ChatView} />
<Stack.Screen name="GroupChat" component={GroupChatView} />
<Stack.Screen name="Friends" component={FriendsView} />
</Stack.Navigator>
What I am trying to achieve is that I could go Posts -> UserProfile -> Chat -> UserProfile -> Posts, but because Posts stack exists on Home tab, and UserProfile and Chat are on Message tab, I can't seem to do it. I can go from Posts to UserProfile and then to Chat and back to UserProfile. When I press back from UserProfile to Posts, the Tab switches to the Home-tab like it should, however if I switch back to Message-tab, the UserProfile-Stack is still open, and calling goBack() doesn't close it, just takes me back to Home-Tab. So basically I can't get back to Messages-stack if I have opened UserProfile-stack from the Home-tab.
And this problem happens only if I start up my application and don't open Message-tab. If Message-tab has been opened, this all functions as it should.
Am I doing something wrong? Is it possible to load the Message-tab when my app is started, or should I go about this some other way?

React Navigation 6.x Initial Route Not Working

I'm using React Navigation 6.x's linking with Expo so that when a user clicks on a notification they are directed to the appropriate part of my application to interact with the new information. When my app is backgrounded (running in the background) and a user clicks on a notification they are redirected to the screen they need to be at, which works perfectly fine. However, when the app is killed and the user clicks on a notification, they are taken directly to the screen for which the url is provided and they cannot press back to navigate elsewhere in my application. I tried to resolve this by using the initialRouteName prop like is shown in the docs (link: https://reactnavigation.org/docs/configuring-links/#rendering-an-initial-route), but I cannot get it to work. For further clarification, when I mentioned above that I am able to get linking working it is in relation to the direct SettingsScreen, AddFriendScreen, and MessagingScreen links. What I cannot get working is the specific block of code inside the liking object that starts with HomeScreen. What I believe may be causing the issue is that I am trying to set my initialRoute as a screen within HomeScreen's Tabs.Navigator and then trying to route to a screen within my Stack.Navigator. However, the docs show that this is possible (link: https://reactnavigation.org/docs/configuring-links/#rendering-an-initial-route).
My code:
const linking = {
prefixes: [prefix],
config: {
screens: {
HomeScreen: {
initialRouteName: "Chats",
screens: {
AddFriendScreen: "addFriend",
CreateChatScreen: "createChatScreen",
Friends: "friends",
MessagingScreen: 'messagingScreen/:chatRoomId'
}
},
SettingsScreen: "SettingsScreen",
AddFriendScreen: "AddFriendScreen",
MessagingScreen: 'MessagingScreen/:chatRoomId'
},
}
};
<NavigationContainer linking={linking} theme={MyTheme} >
<Stack.Navigator>
{!authState.value ?
<>
<Stack.Screen name="LoginScreen" component={LoginScreen} />
<Stack.Screen name="SignUpScreen" component={SignUpScreen} />
<Stack.Screen name="ForgotPasswordScreen" component={ForgotPasswordScreen} />
</>
:
<>
<Stack.Screen name="HomeScreen" component={HomeScreen} />
<Stack.Screen name="MessagingScreen" component={MessagingScreen} />
<Stack.Screen name="SettingsScreen" component={SettingsScreen} />
<Stack.Screen name="CreateChatScreen" component={CreateChatScreen} />
<Stack.Screen name="AddFriendScreen" component={AddFriendScreen} />
</>
}
</Stack.Navigator>
</NavigationContainer>
const HomeScreen = () => {
return (
<Tabs.Navigator>
<Tabs.Screen name="Chats" component={ChatScreen} />
<Tabs.Screen name="Friends" component={FriendsScreen} />
<Tabs.Screen name="Profile" component={ProfileScreen} />
</Tabs.Navigator>
)
}
Hmm, I would put any screen that I would like to access in multiple navigators at the root level so that they can both reach it.
For my own project, I have a root stack with a dedicated auth stack and an app/home stack (which is a tab nav like your HomeScreen). And outside that I have all my modal and root screens that I'd like other navigators to access (Because I really have two Home tab navigators the user can switch between.)
Idk if putting all of your screens listed after HomeScreen outside of that object will help your situation, but you can try that.

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