I am using the latest version of React native. I need to navigate from one Stack .Navigator to another Stack.Navigator.
The structure would be the following:
-Stack.Navigator (LoginStack)
-Stack Screen -> LoginScreen
-Stack.Screen -> CreateAccount
-Stack.Navigator (ProdsStack)
-Stack.Screen -> ProdsScreen
-Stack.Screen -> DetailsScreen
I need to navigate from LoginScreen to ProdsScreen, I've tried Navigation.navigate but it doesn't give me any results.
This is the code for my router:
import React from 'react';
import {NavigationContainer} from '#react-navigation/native';
import {createStackNavigator} from '#react-navigation/stack';
import LoginStack from './src/components/Login/LoginStack';
import {createBottomTabNavigator} from '#react-navigation/bottom-tabs';
import ListProdsStack from './src/components/ListProds/ListProdsStack';
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();
function MainTabScreen() {
return (
<Tab.Navigator>
<Tab.Screen name="ProdListTab" component={ListProdsStack} />
</Tab.Navigator>
);
}
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="LoginStack"
component={LoginStack}
options={{headerShown: false}}
/>
<Stack.Screen
name="ProdListStack"
component={MainTabScreen}
options={{headerShown: false}}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
This is my LoginStack
const Stack = createStackNavigator();
const LoginStack = () => {
return (
<Stack.Navigator
screenOptions={{
headerStyle: {
backgroundColor: '#F2F302',
},
headerTintColor: '#3C2B58',
}}>
<Stack.Screen
name="Login Screen"
component={LoginScreen}
options={{headerShown: false}}
/>
<Stack.Screen name="Cuenta Nueva" component={CreateAccount} />
</Stack.Navigator>
);
};
This is my ListProfsStack:
const Stack = createStackNavigator();
const ListProdsStack = () => {
return (
<Stack.Navigator>
<Stack.Screen name="ProdList" component={ListProdsScreen} />
</Stack.Navigator>
);
};
I was thinking of using different Stacks because each Tab has its own screens which cannot be accessed from one Stack to another Stack directly.
Related
I try to pass flatlist item value to other screen (main navigator), but it always return undefined. I want to achieve that when user onPress(), it will pass the value into the new screen, because I need it in the tab navigator screens. I already try to pass using using routes, but it also return undefined.
App.js
import React, {Component} from 'react';
import 'react-native-gesture-handler';
import {createStackNavigator} from '#react-navigation/stack';
import { NavigationContainer } from '#react-navigation/native';
//import another Page
import AccountSelectScreen from './screens/AccountSelectScreen';
import NewAccountScreen from './screens/NewAccountScreen';
import MainNavigator from './screens/MainNavigator';
import HomeScreen from './screens/HomeScreen';
import VaccineScreen from './screens/VaccineScreen';
import NotificationScreen from './screens/NotificationScreen';
import UserScreen from './screens/UserScreen';
import SplashScreen from './screens/SplashScreen';
import SignUpAccScreen from './screens/SignUpAccScreen';
import RegisterScreen from './screens/RegisterScreen';
import ScanQR from './screens/ScanQR';
const styles = require('./styles/styles');
const Stack = createStackNavigator();
const Auth = (route) => {
// Stack Navigator for Login and Sign up Screen
return (
<Stack.Navigator initialRouteName="SignUpAcc">
<Stack.Screen
name="SignUpAcc"
component={SignUpAccScreen}
options={{headerShown: false}}
/>
<Stack.Screen
name="AccountSelectScreen"
component={AccountSelectScreen}
options={{headerShown: false}}
/>
<Stack.Screen
name="NewAccountScreen"
component={NewAccountScreen}
options={{headerShown: false}}
/>
<Stack.Screen
name="RegisterScreen"
component={RegisterScreen}
options={{headerShown: false}}
/>
</Stack.Navigator>
);
};
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="SplashScreen">
<Stack.Screen
name="SplashScreen"
component={SplashScreen}
// Hiding header for Splash Screen
options={{headerShown: false}}
/>
{/* Auth Navigator: Include Login and Signup */}
<Stack.Screen
name="Auth"
component={Auth}
options={{headerShown: false}}
/>
{/* Navigation Drawer as a landing page */}
<Stack.Screen
name="MainNavigator"
component={MainNavigator}
// Hiding header for Navigation Menu
options={{headerShown: false}}
/>
<Stack.Screen
name="BarcodeScan"
component={ScanQR}
options={{headerShown: false}}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
renderItem flatlist
const renderItem = ({ item }) => (
<View style={{marginBottom: 20}}>
<Pressable
style={styles.baby_box}
title={item.nameVal}
>
<Text>{item.nameVal}</Text>
</Pressable>
<Pressable
style={styles.btnDelete}
value={item.nameVal}
onPress={() => {
// selectChild(text)
navigation.navigate('MainNavigator',{
nameChild: item.nameVal
});
}}
>
<Text style={styles.btnText}>Pilih</Text>
</Pressable>
</View>
);
const MainNavigator = (route,navigation) => {
const {nameChild} = route.params;
}
export default MainNavigator;
From this given code i think you are destructuring props in the wrong way thats why its undefined.
const MainNavigator = (route,navigation) => {
const {nameChild} = route.params;
}
export default MainNavigator;
Try this.
const MainNavigator = (props) => {
const {nameChild} = props.route.params;
}
export default MainNavigator;
You have to add curly braces so it takes parameters with in the object
Like this:({route,navigation})
const MainNavigator = ({route,navigation}) => {
const {nameChild} = route.params;
}
export default MainNavigator;
I would like to have stack and drawer navigators use the same navigation bar. However, both are creating their own navigation bars refer to the below image making the app look messy. How can I achieve that?
Below is my code:
import * as React from "react";
import { createStackNavigator } from "#react-navigation/stack";
import { createDrawerNavigator } from "#react-navigation/drawer";
import Home from "../screens/Home";
import Contact from "../screens/Contact";
import About from "../screens/About";
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
const HomeNavigation = (props) => {
return (
<Stack.Navigator initialRouteName='Home'
screenOptions={{
headerTitleAlign:'center',
//headerShown: false
}}
>
<Stack.Screen name='Home' component={Home} />
<Stack.Screen name='Contact' component={Contact} />
<Stack.Screen name='About' component={About} />
</Stack.Navigator>
);
};
const MenuNavigation = (props) => {
return (
<Stack.Navigator initialRouteName='Home'
screenOptions={{
headerTitleAlign:'center',
//headerShown: false
}}>
<Stack.Screen name='Menu' component={About} />
</Stack.Navigator>
);
};
const DrawerNavigation = () => {
return (
<Drawer.Navigator>
<Drawer.Screen name='HomeNavigation' component={HomeNavigation} />
<Drawer.Screen name='MenuNavigation' component={MenuNavigation} />
</Drawer.Navigator>
);
};
export default DrawerNavigation;
I make a Navigation component and put all screen in it.
it working well, but props in Tab.Navigation not work such as barStyle,initialRouteName,....
I make stack for each screen and put all stack in bottom navigation and i wanna change option for bottom navigation but I cant.
I set initialRouteName and bar style but now working at all.
Thanks for response.
import {NavigationContainer} from '#react-navigation/native';
import {createBottomTabNavigator} from '#react-navigation/bottom-tabs';
import CollectionScreen from '../screens/Collection';
import LiveScreen from '../screens/Live';
import PrayScreen from '../screens/Pray';
import ProfileScreen from '../screens/Profile';
import PublicationScreen from '../screens/Publication';
import React from 'react';
import {createNativeStackNavigator} from '#react-navigation/native-stack';
const LiveStack = createNativeStackNavigator();
function LiveStackScreen() {
return (
<LiveStack.Navigator screenOptions={{headerShown: false}}>
<LiveStack.Screen name="LiveScreen" component={LiveScreen} />
</LiveStack.Navigator>
);
}
const ProfileStack = createNativeStackNavigator();
function ProfileStackScreen() {
return (
<ProfileStack.Navigator screenOptions={{headerShown: false}}>
<ProfileStack.Screen name="ProfileScreen" component={ProfileScreen} />
</ProfileStack.Navigator>
);
}
const PrayStack = createNativeStackNavigator();
function PrayStackScreen() {
return (
<PrayStack.Navigator screenOptions={{headerShown: false}}>
<PrayStack.Screen name="PrayScreen" component={PrayScreen} />
</PrayStack.Navigator>
);
}
const PublicationStack = createNativeStackNavigator();
function PublicationStackScreen() {
return (
<PublicationStack.Navigator screenOptions={{headerShown: false}}>
<PublicationStack.Screen
name="PublicationScreen"
component={PublicationScreen}
/>
</PublicationStack.Navigator>
);
}
const CollectionStack = createNativeStackNavigator();
function CollectionStackScreen() {
return (
<CollectionStack.Navigator screenOptions={{headerShown: false}}>
<CollectionStack.Screen
name="CollectionScreen"
component={CollectionScreen}
/>
</CollectionStack.Navigator>
);
}
const Tab = createBottomTabNavigator();
export default function Navigation() {
return (
<NavigationContainer>
<Tab.Navigator
// This props not working
initialRouteName="Profile"
barStyle={{backgroundColor: '#694fad'}}>
<Tab.Screen name="Live" component={LiveStackScreen} />
<Tab.Screen name="Profile" component={ProfileStackScreen} />
<Tab.Screen name="Pray" component={PrayStackScreen} />
<Tab.Screen name="Publication" component={PublicationStackScreen} />
<Tab.Screen name="Collection" component={CollectionStackScreen} />
</Tab.Navigator>
</NavigationContainer>
);
}
I know why this snippets not working.
I wrote the code like below
initialRouteName="Profile"
But we must set initialRouteName like below
initialRouteName={'Profile'}
and for changing background color of bottomTab you should do like below
screenOptions={{
headerShown: false,
tabBarStyle: {backgroundColor: 'red'},
}}
So i was creating RN project which had multiple stack navigator
Logged In Routes
Non Logged in routes
So I have non logged in routes like this
import React from "react"
import { createNativeStackNavigator } from "react-native-screens/native-stack"
import { Introduction, Login } from "#src/screens/loggedOut"
const Stack = createNativeStackNavigator()
const defaultOptions = {
headerShown: false
}
const LoggedOutRoutes = () => {
return (
<Stack.Navigator initialRouteName="introduction">
<Stack.Screen name="introduction" component={Introduction} options={defaultOptions} />
<Stack.Screen name="login" component={Login} options={defaultOptions} />
</Stack.Navigator>
)
}
export default LoggedOutRoutes
And similarly logged in routes
import React from "react"
import { createNativeStackNavigator } from "react-native-screens/native-stack"
import {Home,Settings } from "#src/screens/loggedIn"
const Stack = createNativeStackNavigator()
const defaultOptions = {
headerShown: false
}
const LoggedIn = () => {
return (
<Stack.Navigator initialRouteName="home">
<Stack.Screen name="home" component={Home} options={defaultOptions} />
<Stack.Screen name="settings" component={Settings} options={defaultOptions} />
</Stack.Navigator>
)
}
export default LoggedIn
Now, I want to load either of these roots based on if user is logged in or not.
How would I do it?
Assuming you have a login state saved, you can wrap your navigation in the NavigationContainer component from "#react-navigation/native" and render the correct navigation conditionally. Something like this:
<NavigationContainer>
{userLoggedIn ? <LoggedIn /> : <LoggedOutRoutes />}
</NavigationContainer>
In the latest version of react-navigation v5 how to combine createStackNavigator with createDrawerNavigator
This is my main navigator file.
import InitialScreen from '#InitialScreen'
import SignIn from '#SignIn'
import Home from '#Home'
import {NavigationContainer} from '#react-navigation/native';
import {createStackNavigator} from '#react-navigation/stack';
import { createDrawerNavigator } from '#react-navigation/drawer';
const Drawer = createDrawerNavigator();
const Stack = createStackNavigator();
class NavigationStack extends Component {
render() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="InitialScreen" component={InitialScreen} options={{ headerShown: false }}/>
<Stack.Screen name="SignIn" component={SignIn} options={{ headerShown: false }} />
<Stack.Screen name="Home" component={Home} options={{ headerShown: false }} />
</Stack.Navigator>
</NavigationContainer>
);
}
}
export default NavigationStack;
In my Home component I have bottom-tabs
import { NavigationContainer } from '#react-navigation/native';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
const Tab = createBottomTabNavigator();
class Home extends Component {
render() {
return (
<Tab.Navigator>
<Tab.Screen
name="Documents"
component={Images}
options={{
tabBarLabel: 'Images',
tabBarIcon: ({ focused }) => (
<Image source={Images.iconDocuments}/>
),
}}
/>
<Tab.Screen
name="Profile"
component={Profile}
options={{
tabBarLabel: 'IRROP',
tabBarIcon: ({ focused }) => (
<Image source={Images.iconImages}/>
),
}}
/>
</Tab.Navigator>
)
}
}
export default Home;
I want to include Drawer Navigation in Stack Navigation How can I do it in react-navigation v5