React Native - Open Drawer not working when using Navigation.push() - react-native

I am working on a React Native Expo app where I have both Stack Navigation and Drawer Navigation.
I have nested Drawer Navigator inside my Stack Navigator in app.js. When I click on Drawer hamburger menu, it opens the drawer from the right hand side. It was working fine on all the screens without any issue.
The problem occured when I decided to use this.props.navigation.push("Payment") to push to payment screen.
OpenDrawer() function is not passed as part of a prop when using navigation.push() whereas its working fine when using navigation.navigate().
OpenDrawer() function works in the payment screen when I navigate to that screen using below statement
this.props.navigation.navigate("Payment");
OpenDrawer() function throws function not found error when I navigate to that screen using below statement
this.props.navigation.push("Payment");
Below is my app.js where i nested drawer inside stack navigator. Can someone please advise how to resolve the issue. Thanks !!!
app.js
import React from "react";
import {
Dimensions,
ScrollView,
Button,
View,
SafeAreaView,
} from "react-native";
import { createStackNavigator } from "#react-navigation/stack";
import {
createDrawerNavigator,
DrawerContentScrollView,
} from "#react-navigation/drawer";
import {
NavigationContainer,
useNavigation,
DrawerItem,
} from "#react-navigation/native";
import Landingzone from "./components/Landingzone";
import LandingPage from "./components/LandingPage";
import Summary from "./components/Summary";
import Payment from "./components/Payment";
import { TouchableOpacity } from "react-native-gesture-handler";
import { Icon, Text } from "react-native-elements";
const myFont = Platform.OS === "ios" ? "Arial" : "sans-serif";
let myFontSize = 15;
const SCREEN_WIDTH = Dimensions.get("window").width;
if (SCREEN_WIDTH > 300 && SCREEN_WIDTH <= 360) {
myFontSize = 10;
} else if (SCREEN_WIDTH > 300 && SCREEN_WIDTH <= 415) {
}
export default function App() {
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
const DrawerNavigator = () => (
<Drawer.Navigator
drawerPosition="right"
drawerContentOptions={{
labelStyle: {
color: "white",
fontFamily: myFont,
fontSize: 16,
},
}}
drawerStyle={{
backgroundColor: "#343a40",
flex: 1,
flexDirection: "column",
}}
drawerContent={(props) => (
<DrawerContentScrollView
contentContainerStyle={{
flex: 1,
flexDirection: "column",
}}
>
<View
style={{
flex: 0.15,
}}
>
<Icon
reverse
name="user-circle-o"
type="font-awesome"
color="#517fa4"
containerStyle={{
backgroundColor: "green",
left: 115,
}}
/>
<Text>SaimugaTutorials#gmail.com</Text>
</View>
<View
style={{
flex: 0.1,
justifyContent: "center",
}}
>
<Text>Enroll</Text>
</View>
</DrawerContentScrollView>
)}
>
<Drawer.Screen
name="Summary"
component={Summary}
options={{ headerShown: false }}
/>
<Drawer.Screen
name="Payment"
component={Payment}
options={{ headerShown: false }}
/>
</Drawer.Navigator>
);
const StackNavigator = () => (
<Stack.Navigator initialRouteName="Welcome">
<Stack.Screen
name="Welcome"
component={LandingPage}
options={{ headerShown: false }}
/>
<Stack.Screen
name="Payment"
component={Payment}
options={{ headerShown: false }}
/>
<Stack.Screen
name="Summary"
component={DrawerNavigator}
options={{ headerShown: false, gestureEnabled: false }}
/>
</Stack.Navigator>
);
return (
<NavigationContainer>
<StackNavigator />
</NavigationContainer>
);
}

Related

toggleDrawer from #react-navigation/drawer not working in React-Native

I made a nested navigation Drawer + Stack, but the drawer is not working, I am not able to toggle the drawer, but able to drag the drawer from the left side and it goes back closing automatically after releasing it.
Also, I get a pop up when hovering over navigation.toggleDrawer(), this is what I get
Property 'toggleDrawer' does not exist on type
'NavigationProp<ParamListBase, string, Readonly<{ key: string; index:
number; routeNames: string[]; history?: unknown[]; routes:
NavigationRoute<ParamListBase, string>[]; type: string; stale: false;
}>, {}, {}>'
So what's wrong here and how to fix it? And can I make a drawer without using #react-navigation/drawer ?
app.js :
import React from "react";
import { NavigationContainer } from "#react-navigation/native";
import { createStackNavigator } from "#react-navigation/stack";
import HomeScreen from "./src/screens/HomeScreen";
import MovieScreen from "./src/screens/MovieScreen";
import FavoriteScreen from "./src/screens/FavoritesScreen";
import { useFonts } from "expo-font";
import AppLoading from "expo-app-loading";
import { createDrawerNavigator } from "#react-navigation/drawer";
import CustomDrawerContent from "./src/components/DrawerContent";
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
function HomeStackScreen() {
return (
<Stack.Navigator>
<Stack.Screen
name='home'
component={HomeScreen}
options={{ headerShown: false }}
/>
<Stack.Screen
name='movie'
component={MovieScreen}
options={{ headerShown: false }}
/>
</Stack.Navigator>
);
}
export default () => {
const [fontLoaded] = useFonts({
Regular: require("./assets/fonts/NunitoSans-Regular.ttf"),
Bold: require("./assets/fonts/NunitoSans-Bold.ttf"),
Black: require("./assets/fonts/NunitoSans-Black.ttf"),
ExtraBold: require("./assets/fonts/NunitoSans-ExtraBold.ttf"),
ExtraLight: require("./assets/fonts/NunitoSans-ExtraLight.ttf"),
Light: require("./assets/fonts/NunitoSans-Light.ttf"),
SemiBold: require("./assets/fonts/NunitoSans-SemiBold.ttf"),
});
return fontLoaded ? (
<NavigationContainer>
<Drawer.Navigator
drawerContent={(props) => <CustomDrawerContent {...props} />}
initialRouteName='Home'
screenOptions={{ drawerPosition: "left", drawerType: "front" }}
>
<Drawer.Screen
name='home'
component={HomeStackScreen}
options={{ headerShown: false }}
/>
<Drawer.Screen
name='fav'
component={FavoriteScreen}
options={{ headerShown: false }}
/>
</Drawer.Navigator>
</NavigationContainer>
) : (
<AppLoading />
);
};
home.js
import { useNavigation } from "#react-navigation/native";
export default function HomeScreen(){
const navigation = useNavigation();
return(
<ScrollView style={styles.container}>
<StatusBar
style='auto'
translucent={false}
backgroundColor={COLORS.BASIC_BACKGROUND}
/>
<View
style={{
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
paddingHorizontal: 20,
paddingVertical: 10,
}}
>
<Pressable
style={{ flex: 1 }}
onPress={() => {
navigation.toggleDrawer();
console.log("Clicked on Menu Bar");
}}
>
<Image
style={{ width: 25, height: 28 }}
source={{
uri: "https://img.icons8.com/material-rounded/96/000000/menu--v1.png",
}}
/>
</Pressable>
</View>
</ScrollView>
)
}

How to define routes in React Native without displaying them in a navigator?

I have a bottom tab navigator made with React Navigation on a basic React Native app. It only gives access to 4 screens. Some screens may contains buttons that would open a new page (for example: a user profile page). How to define the route without displaying it in the bottom tab navigator?
The code:
const Tab = createBottomTabNavigator();
export default function Router() {
const { t } = useTranslation();
return (
<Tab.Navigator initialRouteName={screen.home}>
<Tab.Screen name={screen.home} component={Home}/>
<Tab.Screen name={screen.a} component={A}/>
<Tab.Screen name={screen.b} component={B}/>
<Tab.Screen name={screen.c} component={C}/>
</Tab.Navigator>
);
}
The idea would be to allow a click like that:
function ScreenB(){
const navigation = useNavigation();
return (
<TouchableOpacity onPress={() => navigation.navigate("/user/123")}>
<Text>see user 123</Text>
</TouchableOpacity>
)
}
Thanks for your help!
Online example https://snack.expo.io/#vasylnahuliak/great-bubblegum
import * as React from 'react';
import { Button, Text, View } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
const HomeScreen = ({ navigation }) => {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home screen</Text>
</View>
);
};
const ProfileScreen = ({ navigation }) => {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details')}
/>
</View>
);
};
const DetailsScreen = () => { // <--- Screen without navigation header
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Details screen</Text>
</View>
);
};
const ProfileStack = createStackNavigator();
const ProfileStackScreen = () => {
return (
<ProfileStack.Navigator>
<ProfileStack.Screen name="Settings" component={ProfileScreen} />
</ProfileStack.Navigator>
);
};
const MainTab = createBottomTabNavigator();
const MainTabs = () => {
return (
<MainTab.Navigator>
<MainTab.Screen name="Home" component={HomeScreen} />
<MainTab.Screen name="Profile" component={ProfileStackScreen} />
</MainTab.Navigator>
);
};
const RootStack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<RootStack.Navigator>
<RootStack.Screen
name="MainTabs"
component={MainTabs}
options={{ headerShown: false }}
/>
<RootStack.Screen name="Details" component={DetailsScreen} /> // <--- Screen without navigation header
</RootStack.Navigator>
</NavigationContainer>
);
}
Official documentation: https://reactnavigation.org/docs/hiding-tabbar-in-screens

Navigate to other StackNavigator screen when press button on navbar

I'm pretty new to react and this is my first app.
I have a stack navigator with 2 screens by now: MainMenu and Profile. While the user is in MainMenu, a button on top right corner is shown and I need to redirect the user to the Profile screen when this button is pressed. I need something like this.props.navigation.navigate('Profile') but this does not work, because this, props and navigation are not defined.
My thinks are that I cannot redirect to Profile from this stack navbar, cause Profile is still defined yet, but I don't know another way to do it.
// mainStack.js
import React from 'react';
import { View, Text, TouchableOpacity, Image } from 'react-native';
import { createStackNavigator } from '#react-navigation/stack';
import MainMenu from '../../screens/home/mainMenu';
import Profile from '../../containers/profileContainer';
import Icon from 'react-native-vector-icons/FontAwesome';
import { useSelector } from 'react-redux';
const MainStack = () => {
const Stack = createStackNavigator();
const isAdmin = (useSelector(state => state.auth.user.role) === 'admin');
function renderUserMenu() {
return (
<TouchableOpacity style={{ marginRight: 20 }} onPress={() => console.log("HERE I NEED TO REDIRECT TO THE SCREEN PROFILE") } >
<Icon style={{ color: 'white' }} name='user-circle-o' size={30} />
</TouchableOpacity>
)
}
function LogoTitle() {
return (
<Image
style={{ width: 150, height: 50 }}
source={require('../../assets/logo-with-slogan.png')}
/>
);
}
function renderConfigBtn(_isAdmin) {
if (!_isAdmin) {
return (
<TouchableOpacity style={{ marginRight: 10 }} onPress={() => console.log('Configuraciones')} >
<Icon style={{ color: 'white' }} name='cog' size={30} />
</TouchableOpacity>
)
}
}
return (
<Stack.Navigator>
<Stack.Screen
name="MainMenu"
component={MainMenu}
options={{
headerTitle: props => <LogoTitle {...props} />,
headerRight: () => (
<View style={{ flexDirection: 'row' }}>
{renderConfigBtn(isAdmin)}
{renderUserMenu()}
</View>
),
headerStyle: { backgroundColor: '#0064c8' },
}}
/>
<Stack.Screen
name="Profile"
component={Profile}
options={{
headerStyle: { backgroundColor: '#0064c8' },
}}
/>
</Stack.Navigator>
)
}
export default MainStack;
Also, this stack is inside a navigation container as follows:
import React from 'react';
import { useSelector } from "react-redux";
import { NavigationContainer } from "#react-navigation/native";
import AuthStack from "./authStack";
import MainStack from "./mainStack";
const AppNavigator = props => {
const isAuth = useSelector(state => !!state.auth.access_token);
return (
<NavigationContainer>
{ !isAuth && <AuthStack/>}
{ isAuth && <MainStack/>}
</NavigationContainer>
);
};
export default AppNavigator;
I would appreciate any help.
You can access 'navigation' in options like below
options={({navigation})=>({
headerTitle: props => <LogoTitle {...props} />,
headerRight: () => (
<View style={{ flexDirection: 'row' }}>
{renderConfigBtn(isAdmin,navigation)}
{renderUserMenu(navigation)}
</View>
),
headerStyle: { backgroundColor: '#0064c8' },
})}
Basically you can pass a function as a prop to options and navigation will be passed to it as a parameter.
function renderUserMenu(navigation) {
return (
<TouchableOpacity style={{ marginRight: 20 }} onPress={() => navigation.navigate('YOUR SCREEN') } >
<Icon style={{ color: 'white' }} name='user-circle-o' size={30} />
</TouchableOpacity>
)
}
And you can change the renderUserMenu function like above so that it will do the navigation as required.
Use navigation options and then pass it to the function to navigate to profile:
<Stack.Screen
name="MainMenu"
component={MainMenu}
options={({ navigation }) => ({ ......
We simply can import the useNavigation hook from the react-navigation/native package and can implement navigation with the use of this hook without accessing the navigation props from the component.
For Ex.
First import the hook,
import { useNavigation } from '#react-navigation/native';
Use the hook to implement navigation as below in MainStack.js:
const navigation = useNavigation();
function renderUserMenu() {
return (
<TouchableOpacity style={{ marginRight: 20 }} onPress={() => navigation.navigate("Profile") } >
<Icon style={{ color: 'white' }} name='user-circle-o' size={30} />
</TouchableOpacity>
)
}

adjusting Switch and Icon in stack header on react-navigation

I was trying to put an Switch and an Icon on header created with stack navigation from react navigation. the problem I am facing is that adjusting the switch and icon accordingly next to each other. I tried different alternatives but still I am unable to adjust them. I wanted to show on the header a text in the center; Home for instance, and show also a switch and icon on the right of the header next to each other (switch and icon). I will highly appreciate any help of how Can I do this? Here I am sharing my code trying to do it.
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* #format
* #flow strict-local
*/
import React, {useState} from 'react';
import {StatusBar, View, TouchableOpacity} from 'react-native';
import {
NavigationContainer,
DarkTheme as navigationDarkTheme,
DefaultTheme as navigationDefaultTheme,
useTheme
} from '#react-navigation/native';
import {createStackNavigator} from '#react-navigation/stack';
import {
DarkTheme as PaperDarkTheme,
Provider as PaperProvider,
DefaultTheme as PaperDefaultTheme,
Text,
Title,
TouchableRipple,
Switch,
} from 'react-native-paper';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
const customDarktheme = {
...PaperDarkTheme,
...navigationDarkTheme,
colors: {
...PaperDarkTheme.colors,
...navigationDarkTheme.colors,
headerColor: 'rgb(255, 255, 255)',
bgcolor: "#404040",
surface:"#404040",
btnSearchColor:"#404040",
}
}
const customDefaulttheme = {
...PaperDefaultTheme,
...navigationDefaultTheme,
colors: {
...PaperDefaultTheme.colors,
...navigationDefaultTheme.colors,
headerColor: "white",
bgcolor: "#fafcff",
btnSearchColor:"#006aff",
surface:"white",
}
}
const HomeS = () => {
return <View></View>;
};
const Stack = createStackNavigator();
const App = () => {
const {colors} = useTheme()
const [isDarktheme, setDarkTheme] = useState(false);
const togglemethod = () => {
setDarkTheme(!isDarktheme);
};
return (
<>
<PaperProvider theme={isDarktheme?customDarktheme:customDefaulttheme}>
<NavigationContainer theme={isDarktheme?customDarktheme:customDefaulttheme}>
<StatusBar barStyle="dark-content" />
<Stack.Navigator screenOptions={{headerTitleAlign: 'center', headerStyle: { backgroundColor: colors.headerColor }}}>
<Stack.Screen
name="Home"
component={HomeS}
options={{
headerTitle: (props) => (
<View style={{flexDirection: 'row', width:"300%"}}>
<>
<View>
<Title style={{paddingLeft: 180}}>
<Text>Home</Text>
</Title>
</View>
<View >
<TouchableRipple rippleColor="rgba(0, 0, 0, .32)">
<Switch
value={isDarktheme}
color="yellow"
onValueChange={() => togglemethod()}
style={{
paddingLeft:250,
}}
/>
</TouchableRipple>
</View>
<View style={{}}>
<MaterialCommunityIcons
name={
isDarktheme
? 'moon-waning-crescent'
: 'white-balance-sunny'
}
size={25}
color={isDarktheme ? "yellow" : "blue"}
style={{
paddingLeft: 110,
// paddingBottom: 5,
width: '200%',
flexDirection: 'row',
paddingRight:300
}}
/>
</View>
</>
</View>
),
}}
/>
</Stack.Navigator>
</NavigationContainer>
</PaperProvider>
</>
);
};
export default App;
currently the header looks like this:
look at the distance b/w the switch and Icon. trying to eliminate this was not possible for me. for example the text Home disappears while adjusting other elements like switch or Icon. I know this can be achieved. but I run out of options and glad that someone else can do it and learn from.
Since you want to add the switch and icon on the right, you should use headerRight instead of headerTitle
options={{
headerRight: () => (
<View style={{ flexDirection: 'row', justifyContent: 'flex-end' }}>
// Your switch and icon here
</View>
),
}}

BottomTabNavigator not working in React Native

I am using Drawer,Stack and Tab navigator using React Navigation 5, I have followed documentation but Tab Navigator not showing.
Here's my code:
In my app.js I have called my main StackNavigator:
const Stack = createStackNavigator();
export default class App extends React.Component {
render() {
return (
<NavigationContainer>
<Stack.Navigator headerMode="none" initialRouteName="HomePage">
<Stack.Screen name="Home" component={HomePage} />
<Stack.Screen name="Login" component={LoginSignUp} />
<Stack.Screen name="DrawerScreenss" component={DrawerScreens} />
<Stack.Screen name="Tab" component={TabComp} />
</Stack.Navigator>
</NavigationContainer>
);
}
}
This is my Drawer Navigation Code:
import * as React from 'react';
import { createDrawerNavigator } from '#react-navigation/drawer';
import DrawerContent from './DrawerComponents/DrawerContent';
import DrawerHome from './DrawerComponents/DrawerHome';
const Drawer = createDrawerNavigator();
export default class DrawerScreens extends React.Component {
render() {
return (
<Drawer.Navigator
drawerContent={() => (
<DrawerContent navigation={this.props.navigation} />
)}>
<Drawer.Screen name="DrawHome" component={DrawerHome} />
</Drawer.Navigator>
);
}
}
Here's my TabNavigator which is not working:
import React, { Component } from "react";
import { createBottomTabNavigator } from "#react-navigation/bottom-tabs";
import DrawerHome from "./DrawerHome";
import Bookmarks from "./Bookmarks";
export default class TabComp extends Component {
render() {
const Tab = createBottomTabNavigator();
return (
<Tab.Navigator
initialRouteName="Home"
tabBarOptions={{
activeTintColor: "#e91e63",
activeTintColor: "red",
inactiveTintColor: "grey",
style: {
backgroundColor: "white",
borderTopColor: "red"
},
labelStyle: {
fontSize: 12,
fontWeight: "normal"
},
indicatorStyle: {
borderBottomColor: "red",
borderBottomWidth: 4
}
}}
>
<Tab.Screen name="Home" component={DrawerHome} />
<Tab.Screen name="Bookmarks" component={Bookmarks} />
</Tab.Navigator>
);
}
}
It should be visible in my DrawerHome screens but not working
I don't know exactly what your HomePage component looks like, but it could look something like this with a button that navigates to your DrawerScreens screen:
const HomePage = ({navigation}) => {
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text>Feed Screen</Text>
<Button
title="Go to DrawerScreens"
onPress={() => navigation.navigate('DrawerScreens')}
/>
</View>
);
};
The important thing I changed in your DrawerScreens component is that its screen is your TabComp component:
class DrawerScreens extends React.Component {
render() {
return (
<Drawer.Navigator
drawerContent={() => (
<DrawerContent navigation={this.props.navigation} />
)}>
<Drawer.Screen name="Tab" component={TabComp} />
</Drawer.Navigator>
);
}
}
The TabComp component itself can stay the same.
Then inside your DrawerHome component you can trigger your drawer using navigation.openDrawer().
const DrawerHome = ({navigation}) => {
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text>Feed Screen</Text>
<Button title="Open Drawer" onPress={() => navigation.openDrawer()} />
</View>
);
};
This way you can show your tabs and your drawer at the same time.