Navigate from a Bottom Tab Navigator to a Stack Navigator - react-native

I am building an app with the following dependencies:
"dependencies": {
"#react-navigation/bottom-tabs": "^5.8.0",
"#react-navigation/compat": "^5.2.5",
"#react-navigation/material-bottom-tabs": "^5.2.16",
"#react-navigation/material-top-tabs": "^5.2.16",
"#react-navigation/native": "^5.7.3",
"#react-navigation/stack": "^5.9.0",
"expo": "~38.0.8",
"expo-status-bar": "^1.0.2",
"react": "~16.11.0",
"react-dom": "~16.11.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz",
"react-native-elements": "^2.0.4",
"react-native-gesture-handler": "^1.7.0",
"react-native-modals": "^0.19.9",
"react-native-reanimated": "^1.10.2",
"react-native-screens": "^1.0.0-alpha.23",
"react-native-web": "~0.11.7",
"react-navigation": "^4.4.0",
"react-navigation-stack": "^2.8.2",
"react-navigation-tabs": "^2.9.0"
},
Let say I have a Stack navigator and a Bottom Tab Navigator. How can I easily navigate from a screen of the bottom tab to a screen of the Stack Navigator?
I found a "solution" which is to add App in my Bottom Tab Navigator, but the problem is it's appearing in the bottom screens, while I dont want that.
Whats the best way to do this?
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer independent={true}>
<Stack.Navigator initialRouteName="Index">
<Stack.Screen name="MyNotes" component={MyNotes} />
<Stack.Screen name="Login" component={Login} />
<Stack.Screen name="Signup" component={Signup} />
<Stack.Screen name="Index" component={Index} />
<Stack.Screen name="PasswordForgot" component={PasswordForgot} />
<Stack.Screen name="BottomNavigation" component={BottomNavigation} />
<Stack.Screen name="ProfileParameters" component={ProfileParameters} />
<Stack.Screen name="MyProfile" component={MyProfile} />
</Stack.Navigator>
</NavigationContainer>
);
}
And a Bottom Tab Navigator:
const Tab = createBottomTabNavigator();
const tabActiveColor = "#EF2D56";
const tabInActiveColor = "#898A8D";
const BottomTabNavigator = () => {
return (
<Tab.Navigator
initialRouteName="MyNotes"
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let tabIcon = "../assets/notes.png";
if (route.name === "MyNotes") {
tabIcon = require("../assets/notes.png");
} else if (route.name === "Feed") {
tabIcon = require("../assets/feed.png");
} else if (route.name === "Discover") {
tabIcon = require("../assets/decouvrir.png");
} else if (route.name === "MyProfile") {
tabIcon = require("../assets/myprofile.png");
}
return (
<Image
source={tabIcon}
resizeMode="contain"
style={{
height: 26.4,
width: 22,
tintColor: focused ? tabActiveColor : tabInActiveColor,
}}
/>
);
},
})}
tabBarOptions={{
style: { zIndex: 110 },
safeAreaInset: { bottom: "never" },
activeTintColor: "#000000",
}}
>
<Tab.Screen
name="MyNotes"
component={MyNotes}
options={{
tabBarLabel: "Notes",
}}
/>
<Tab.Screen
name="Feed"
component={Feed}
options={{
tabBarLabel: "Feed",
}}
/>
<Tab.Screen
name="Discover"
component={Discover}
options={{
tabBarLabel: "Découvrir",
}}
/>
<Tab.Screen
name="MyProfile"
component={MyProfile}
options={{
tabBarLabel: "Profil",
}}
/>
<Tab.Screen name="App" component={App} />
</Tab.Navigator>
);
};
export default BottomTabNavigator;
const Stack = createStackNavigator();
export default function BottomNavigation() {
return (
<NavigationContainer independent={true}>
<BottomTabNavigator />
</NavigationContainer>
);
}
Example of Navigation I want to do: Navigate from the MyProfile screen of the Bottom Tab, to the ProfileParameter screen in the stack navigator.

here is a demo: https://snack.expo.io/#nomi9995/1826cf
use only one NavigationContainer and make bottom tabs part of stack navigator then you can easily move from bottom tabs to stack screens
like this
import * as React from 'react';
import { Text, View, Button } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
function TestScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Test!</Text>
</View>
);
}
function HomeScreen(props) {
const gotoTestStackScreen = () => {
props.navigation.navigate('Test');
};
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
<Button title="Go to test stack screen" onPress={gotoTestStackScreen} />
</View>
);
}
function SettingsScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
}
const Tab = createBottomTabNavigator();
function MyTabs() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
);
}
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Tabs">
<Stack.Screen name="Test" component={TestScreen} />
<Stack.Screen name="Tabs" component={MyTabs} />
</Stack.Navigator>
</NavigationContainer>
);
}

Related

React Navigation, React native, Nested Navigator, Want to Navigate to root of stack

I have a nested navigator, Drawer navigator then stack navigator,
When user is on a screen on the home stack, other than the root screen of the home stack, when user opens drawer and click on Home, it should redirect to the root of the home stack.
How can I achieve this?
Below is code for my drawer navigator and Home stack
drawer:
import {
createDrawerNavigator,
DrawerItemList,
} from "#react-navigation/drawer";
import Bg from "../../assets/background.png";
import { NavigationContainer } from "#react-navigation/native";
import Home from "../screens/home";
import { ImageBackground, SafeAreaView, Text, View } from "react-native";
import HomeStack from "./homeStack";
import { SafeAreaProvider } from 'react-native-safe-area-context';
import SongStack from "./songStack";
import SearchStack from "./searchStack";
import ContactUsStack from "./contactUsStack";
import HelpStack from "./helpStack";
import VolunteerStack from "./volunteerStack";
const CustomDrawerContentComponent = (props) => (
<ImageBackground style={{ flex: 1, resizeMode: "cover" }} source={Bg}>
<View
style={{
backgroundColor: "rgba(59, 82, 88,0.65)",
width: "100%",
height: "100%",
}}
>
<SafeAreaView>
<View style={{ height: "10%" }}></View>
<DrawerItemList {...props} />
</SafeAreaView>
<Text
style={{ position: "absolute", bottom: 1, left: 1, color: "#ffffff" }}
>
Copyright © Songs Of Zion v4.1.1 {new Date().getFullYear()}
</Text>
</View>
</ImageBackground>
);
const Drawer = createDrawerNavigator();
export default function Draweri() {
return (
<SafeAreaProvider>
<NavigationContainer>
<Drawer.Navigator
drawerContent={(props) => <CustomDrawerContentComponent {...props} />}
screenOptions={{
unmountOnBlur:true,
headerShown: false,
drawerItemStyle: {
borderColor: "white",
borderBottomWidth: 1.5,
},
drawerActiveTintColor: "#ffffff",
drawerInactiveTintColor: "#ffffff",
drawerActiveBackgroundColor: "rgba(59, 82, 88,0.7)",
drawerLabelStyle: {
fontSize: 18,
marginLeft: 20,
marginTop: 20,
marginBottom: 20,
},
}}
>
<Drawer.Screen name="DHome" options={{title:"Home"}} component={HomeStack} />
<Drawer.Screen name="Song" component={SongStack} />
<Drawer.Screen name="Search" options={{unmountOnBlur: true}} component={SearchStack} />
<Drawer.Screen name="ContactUs"options={{title:"Contact Us"}} component={ContactUsStack} />
<Drawer.Screen name="Help" component={HelpStack} />
<Drawer.Screen name="Volunteer" component={VolunteerStack} />
</Drawer.Navigator>
</NavigationContainer>
</SafeAreaProvider>
);
}
Home Stack:
import { createNativeStackNavigator } from "#react-navigation/native-stack";
import Home from "../screens/home";
import SearchResults from "../shared/screens/search/searchResults";
import Header from "../shared/header";
import Search from "../screens/search";
import Sync from "../screens/home/sync";
import Song from "../shared/screens/song";
const Stack = createNativeStackNavigator();
export default function HomeStack(navigation) {
return (
<Stack.Navigator>
<Stack.Screen
name="Home"
component={Home}
options={{ headerShown: false }}
/>
<Stack.Screen name="SearchResults" component={SearchResults} options={{header: ({navigation}) => <Header navigation={navigation} title={"Search Results"} pop={true} />}}/>
<Stack.Screen name="SongLyrics" component={Song} options={{header: ({navigation}) => <Header navigation={navigation} title={"Song Lyrics"} pop={true} replace={true} />}}/>
<Stack.Screen name="Search" component={Search} options={{header: ({navigation}) => <Header navigation={navigation} title={"Search"} pop={true}/>}}/>
<Stack.Screen name="BookSongLyrics" component={Song} options={{header: ({navigation}) => <Header navigation={navigation} title={"Song Lyrics"} pop={true}/>}}/>
<Stack.Screen name="Sync" component={Sync} options={{ headerShown: false }}
/>
</Stack.Navigator>
);
}
I have tried:
<Drawer.Screen
name="Home"
component={HomeStack}
options={{
drawerLabel: 'Home',
// This will be called when the home button is pressed in the drawer
onPress: ({ navigation }) => {
// Reset the home stack to the 'Home' screen
navigation.reset({
index: 0,
routes: [{ name: 'Home' }],
});
},
}}
/>
....
function Home({ navigation }) {
useFocusEffect(
React.useCallback(() => {
// Reset the home stack to the 'Home' screen when the 'Home' screen becomes the focused screen
navigation.reset({
index: 0,
routes: [{ name: 'Home' }],
});
}, [navigation])
);
// Other component code
}
...
function App() {
return (
<Drawer.Navigator>
<Drawer.Screen
name="DHome"
component={HomeStack}
options={{
title: 'Home',
// This will be called when the home button is pressed in the drawer
onPress: ({ navigation }) => {
// Navigate to the Home screen
navigation.navigate('Home');
},
}}
/>
{/* Other screens in the drawer navigator */}
</Drawer.Navigator>
);
}
...
const CustomDrawerContentComponent = (props) => {
const { navigation } = props;
return (
<View>
<DrawerItemList
{...props}
onItemPress={({ route, focused }) => {
if (route.name === 'DHome') {
// Navigate to the root screen of the home stack when the home button is pressed
navigation.navigate('Home');
} else {
// Navigate to the pressed screen for other buttons
navigation.navigate(route.name);
}
}}
/>
</View>
);
};
const Drawer = createDrawerNavigator();
function App() {
return (
<Drawer.Navigator
drawerContent={(props) => <CustomDrawerContentComponent {...props} />}
>
<Drawer.Screen name="DHome" component={HomeStack} />
{/* Other screens in the drawer navigator */}
</Drawer.Navigator>
);
}
...
import { NavigationContainer } from '#react-navigation/native';
const Drawer = createDrawerNavigator();
function MyDrawer({ navigation }) {
return (
<NavigationContainer
onNavigationStateChange={(prevState, currentState) => {
const currentRouteName = currentState.routes[currentState.index].name;
if (currentRouteName === 'Home') {
// Navigate to the root screen of the home stack when the home button is pressed
navigation.navigate('Home');
} else {
// Navigate to the pressed screen for other buttons
navigation.navigate(currentRouteName);
}
}}
>
<Drawer.Navigator>
<Drawer.Screen name="Home" component={HomeStack} />
<Drawer.Screen name="Song" component={SongStack} />
<Drawer.Screen name="Search" component={SearchStack} />
<Drawer.Screen name="ContactUs" component={ContactUsStack} />
<Drawer.Screen name="Help" component={HelpStack} />
<Drawer.Screen name="Volunteer" component={VolunteerStack} />
</Drawer.Navigator>
</NavigationContainer>
);
}

React native drawer navigation with I18nManager.forceRTL isn't working

Been trying to implement #react-navigation/drawer with #react-navigation/native-stack and forcing the app to be RTL, using Expo.
App.tsx:
import { I18nManager } from 'react-native'
import { SafeAreaProvider } from 'react-native-safe-area-view'
import {useLoading} from '#hooks'
I18nManager.forceRTL(true)
I18nManager.allowRTL(true)
export default function App() {
const isLoadingComplete = useLoading()
if (!isLoadingComplete) {
return null
} else {
return (
<SafeAreaProvider style={{flex:1}}>
<Navigation />
<StatusBar style="dark" />
</SafeAreaProvider>
)
}
}
Navigation.tsx:
import { createNativeStackNavigator, NativeStackNavigationOptions } from '#react-navigation/native-stack'
import { createDrawerNavigator } from '#react-navigation/drawer'
import { NavigationContainer } from '#react-navigation/native'
const Stack = createNativeStackNavigator<RootStackParamList>()
const Drawer = createDrawerNavigator<RootDrawerParamList>()
function RootNavigator() {
return (
<Stack.Navigator screenOptions={{presentation: 'modal', headerShown: false}}>
<Stack.Screen name="Login" component={LoginScreen} />
<Stack.Screen name="Root" component={DrawerNavigator} />
<Stack.Screen name="Customer" component={CustomerScreen} />
</Stack.Navigator>
)
}
function DrawerNavigator() {
return (
<View style={styles.container}>
<Drawer.Navigator initialRouteName='Customers' screenOptions={{headerRight: Logout}}>
<Drawer.Screen name="Home" component={HomeScreen} options={{title: Lang.Navigation.Home}} />
<Drawer.Screen name="Customers" component={CustomersScreen} options={{title: Lang.Navigation.Customers}} />
</Drawer.Navigator>
</View>
)
}
export default function Navigation() {
return (
<NavigationContainer ref={navigationRef} linking={LinkingConfiguration}>
<RootNavigator />
</NavigationContainer>
)
}
Dependencies:
"#react-navigation/drawer": "^6.1.8",
"#react-navigation/native": "^6.0.6",
"#react-navigation/native-stack": "^6.2.5",
"expo": "~44.0.0",
"react": "17.0.1",
"react-native": "0.64.3",
"react-native-safe-area-view": "^1.1.1",
The result i'm getting when clicking on the menu built-in hamburger is that the screen does become grayish but the menu never pops in. Using inspect I found that there is a problem with the menu's "left" and "translateX" settings automatically given by the react navigation plugin.
Everything works well when removing the 'ForceRTL' part, but I need the app the be RTL.
instead of using the following code.
I18nManager.forceRTL(true)
I18nManager.allowRTL(true)
fix for this issue is to use direction:'rtl' in the style of Root level component. sample working code demo https://snack.expo.dev/PIIGNx70v
sample code
import * as React from 'react';
import { Button, View } from 'react-native';
import { createDrawerNavigator } from '#react-navigation/drawer';
import { NavigationContainer } from '#react-navigation/native';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={navigation.openDrawer} title="Open drawer" />
</View>
);
}
const Drawer = createDrawerNavigator();
export default function App() {
return (
<View style={{ flex: 1, direction: 'rtl' }}>
<NavigationContainer>
<Drawer.Navigator drawerPosition="right" initialRouteName="Home">
<Drawer.Screen name="Home" component={HomeScreen} />
</Drawer.Navigator>
</NavigationContainer>
</View>
);
}

is there any way to keep drawer opened after back from stack navigation in react native expo

I create Drawer Navigation and inside it there is stack navigation all I need when back from stack need the drawer to be opened
my code is like this and All I need is to keep the drawer opened after back from any stack screen
const Drawer = createDrawerNavigator();
function DrawerNav({ navigation }) {
// toggleDrawer = () => {
// this.props.navigation.dispatch(DrawerActions.toggleDrawer())
// }
return (
<Drawer.Navigator initialRouteName="Home"
screenOptions={{
headerShown: true,
headerStyle: {
backgroundColor: brand,
},
headerTintColor: primary,
headerTransparent: false,
headerTitle: '',
headerLeftContainerStyle: {
paddingLeft: 20,
},
}}>
<Drawer.Screen name="Home" component={HomeScreen} options={horizontalAnimation}/>
<Drawer.Screen name="RootStack" component={RootStack} />
</Drawer.Navigator>
);
}
const Stack = createStackNavigator();
const RootStack = () => {
return (
<Stack.Navigator
screenOptions={{
headerStyle: {
backgroundColor: brand,
},
headerTintColor: primary,
headerTransparent: true,
headerTitle: '',
headerLeftContainerStyle: {
paddingLeft: 20,
},
}}
>
{storedCredentials ? (
<Stack.Screen name="Home" component={DrawerNav} options={horizontalAnimation}/>
) : (
<>
<Stack.Screen name="Login" component={Login} options={horizontalAnimation}/>
<Stack.Screen name="Signup" component={Signup} options={horizontalAnimation}/>
</>
)}
</Stack.Navigator>
</NavigationContainer>
);
};
these is the installed package
"#react-navigation/drawer": "^6.1.8",
"#react-navigation/native": "^6.0.6",
"#react-navigation/stack": "^6.0.11",
Use this way...
function MyDrawer() {
const dimensions = useWindowDimensions();
return (
<Drawer.Navigator
screenOptions={{
drawerType: 'permanent',
}}
>
{/* Screens */}
</Drawer.Navigator>
);
}
Refer this
<Drawer.Navigator
drawerContent={(props) => <CustomDrawerContent {...props} />}
>
<Drawer.Screen
name="RootStack"
component={RootStack}
/>
</Drawer.Navigator>
customDrawerContent : component that contains items to be listed on the side bar: check this link for that part https://stackoverflow.com/a/64173773/7689878.
To achieve the drawer remaining after navigating back keep only the RootStack(should contain all stack screens) in the Drawer Navigator.

Translate react navigation tabBarLabel in react native

I have couple of screens in one of them i have a materialTopTabNavigator with two tabs, i need to translate tab names for different languages.
i tried using useNAvigation and withTranslation but it seems i don't have access to i18next t function to translate.
const Stack = createStackNavigator();
const Router = () => {
const Tab = createMaterialTopTabNavigator();
function MyAdsTabs() {
return (
<Tab.Navigator
tabBarOptions={{
activeTintColor: colors.darkGray,
labelStyle: {fontSize: 12},
style: {backgroundColor: colors.white, borderTopWidth: 0, elevation: 0, shadowOpacity: 0},
}}>
<Tab.Screen
name="MyActiveAds"
component={MyActiveAds}
options={{tabBarLabel: 'MyActiveAds'}}
/>
<Tab.Screen
name="MyDeactiveAds"
component={MyDeactiveAds}
options={{tabBarLabel: 'MyDeactiveAds'}}
/>
</Tab.Navigator>
);
}
function MainStackScreen() {
return (
<Stack.Navigator initialRouteName={'Splash'} headerMode="none">
<Stack.Screen name="Splash" component={Splash} />
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Setting" component={Setting} />
<Stack.Screen name="Register" component={Register} />
<Stack.Screen name="Terms" component={Terms} />
<Stack.Screen name="Profile" component={Profile} />
<Stack.Screen name="EditProfile" component={EditProfile} />
<Stack.Screen name="Search" component={Search} />
<Stack.Screen name="FavouriteAds" component={FavouriteAds} />
<Stack.Screen options={{title: 'My home'}} name="MyAdsTabs" component={MyAdsTabs} />
</Stack.Navigator>
);
}
renderLoading = () => (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text>KojaChera</Text>
<ActivityIndicator size="large" color={colors.darkerTeal} />
</View>
);
return (
<Provider store={store}>
<PersistGate persistor={persistor} loading={this.renderLoading()}>
<Root>
<NavigationContainer>
<MainStackScreen />
</NavigationContainer>
</Root>
</PersistGate>
</Provider>
);
};
export default Router;
and this is a part of my package.json:
"react": "16.11.0",
"react-i18next": "^11.3.4",
"react-native": "0.62.2",
"#react-native-community/masked-view": "^0.1.9",
"#react-navigation/material-top-tabs": "^5.1.14",
"#react-navigation/native": "5.2.3",
"#react-navigation/stack": "5.2.18",
"i18next": "^19.4.2",
i solved it by passing i18n as a prop into my router component
const App = () => {
return(
<I18nextProvider i18n={ i18n }>
<Router i18n={ i18n } />
</I18nextProvider>
);
}
and used it this way:
<Tab.Screen name="MyDeactiveAds" component={MyDeactiveAds}
options={{ tabBarLabel: i18n.t('myAds:inActiveAds') }}/>

How can I change the title for each screen inside TabNavigator? - React Navigation

My stack navigator
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="PageA" component={PageA} options={{title:'Page=A'}} />
<Stack.Screen name="PageB" component={PageB} options={{title:'Page=B'}} />
<Stack.Screen name="Menu" component={MenuTabNavigator} options={{title:'Menu'}} />
</Stack.Navigator>
</NavigationContainer>
and my tab navigator
const MenuTabNavigator = () => {
return (
<Tab.Navigator>
<Tab.Screen name="PageA" component={PageA} />
<Tab.Screen name="PageB" component={PageB} />
<Tab.Screen name="Menu" component={Menu} />
</Tab.Navigator>
);};
I'm using Tab Navigator with Stack Navigator.
ScreenA, Screen B and Menu screen in my Tabs.
I pass MenuTabNavigator to StackNavigator's Menu Component as you can see.
Problem:
When I use tabs, header title stays 'Menu'.
For example when I touch to PageB on tab, i expect header title should be 'PageB' but it stays 'Menu'.
How can I change header title for screens when i use bottom tabs?
The approach you are using is wrong. if you go this way you have to create three StackNavigators so that you can get three different headers. and then wrap them in a tab navigator. but this is the wrong way to use it.
import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
function HomeScreen({ navigation }) {
navigation.setOptions({ title: 'Home' })
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
}
function SettingsScreen({ navigation }) {
navigation.setOptions({ title: 'Setting' })
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
}
function Menu({ navigation }) {
navigation.setOptions({ title: 'Menu' })
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Menu</Text>
</View>
);
}
const StackHome = () => {
return (
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
);
};
const StackSetting = () => {
return (
<Stack.Navigator>
<Stack.Screen name="Setting" component={SettingsScreen} />
</Stack.Navigator>
);
};
const StackMenu = () => {
return (
<Stack.Navigator>
<Stack.Screen name="Menu" component={Menu} />
</Stack.Navigator>
);
};
const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="PageA" component={StackHome} options={{ title: "Home" }} />
<Tab.Screen name="PageB" component={StackSetting} options={{ title: "Settings"
}}
/>
<Tab.Screen name="Menu" component={StackMenu} options={{ title: "Menu" }} />
</Tab.Navigator>
</NavigationContainer>
);
}
export default App;