How to close drawer on click in React Native Navigation wix V1 - react-native

I am using react native 0.56.0 and React Native Navigation from Wix [V1]. In React native navigation there is an option to enable drawer. Now drawer can be closed if the user chooses some option in drawer menu or click outside the drawer, but I want to close drawer on the X button click. Has anybody found a way to do this?

try this one. its awasome
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import { gestureHandlerRootHOC } from 'react-native-gesture-handler'
AppRegistry.registerComponent(appName, () => gestureHandlerRootHOC(App));

In a custom drawer, there is already a function called closeDrawer inside props.navigation.
Your close button will look like this:
<TouchableOpacity
onPress={props.navigation.closeDrawer}
style={styles.closeButton}
>
<Image source={Images.close} />
</TouchableOpacity>
Here is more code to put things in context:
In App.js
import { createDrawerNavigator } from "#react-navigation/drawer";
import DrawerScreen from "./src/screens/DrawerScreen";
const Drawer = createDrawerNavigator();
function LeftDrawerStack() {
return (
<Drawer.Navigator
drawerContent={(props) => <DrawerScreen {...props}
/>}
>
<Drawer.Screen name={"Menu item 1"} component={ProfileScreen} />
<Stack.Screen name={"Menu item 2")} component={PaymentScreen} />
</Drawer.Navigator>
);
}
In DrawerScreen.js
import React from "react";
import {TouchableOpacity, View, Image, Text} from 'react-native';
import {
DrawerContentScrollView,
DrawerItemList,
DrawerItem,
} from '#react-navigation/drawer';
import styles from './styles/DrawerScreenStyle';
function DrawerScreen(props) {
return (
<DrawerContentScrollView style={styles.container} {...props}>
<View style={styles.drawerHeader}>
<TouchableOpacity
onPress={props.navigation.closeDrawer}
style={styles.closeButton}
>
<Image source={Images.close} />
</TouchableOpacity>
</View>
<DrawerItemList {...props} />
</DrawerContentScrollView>
);
}
export default DrawerScreen;

This works for me:
this.props.navigator.toggleDrawer({
side: 'right',
animated: true,
to: 'closed',
});

Related

How can I solve the problem of data not passing between views in React Native?

I recently started developing apps using React Native.
During development, I needed to move data from a calendar to another view selected by the user.
I used async storage for this process.
But something went wrong.
Please refer to the following link.
And I made it so that I can pass data in the navigation process without using async storage afterwards.
However, this time, I found that nothing appears on the Detail screen even when February 15 is selected on the calendar.
And this was a problem that occurred even if I selected a date other than February 15th.
I don't know how to solve this problem.
I'm new to React Native development, so I don't know how to solve this problem.
This is my code
App.js
import React from 'react';
import {NavigationContainer} from '#react-navigation/native';
import {createNativeStackNavigator} from '#react-navigation/native-stack';
import HomeScreen from './Home';
import DetailScreen from './Detail';
import { AntDesign } from '#expo/vector-icons';
import { Ionicons } from '#expo/vector-icons';
function App() {
const Stack = createNativeStackNavigator();
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen
name="Home"
component={HomeScreen}
options={({}) => ({
headerBackVisible: false,
headerRight: () => (
<AntDesign name="setting" size={24} color="black" />
),
})}
/>
<Stack.Screen name="Detail" component={DetailScreen}
options={({ navigation }) => ({
headerBackVisible: false,
headerLeft: () => (
<Ionicons name="arrow-back" size={24} color="black" onPress={() => navigation.navigate('Home')} />
),
})}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
Home.js
import React from 'react';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {Calendar} from "react-native-calendars";
import {View} from 'react-native';
function HomeScreen({navigation}) {
const markedDates = {
'2023-02-01': {marked: true, dotColor: 'red'},
}
return(
<SafeAreaProvider>
<View>
<Calendar
markedDates={markedDates}
onDayPress={(day) => {
navigation.push('Detail', {day});
}}
/>
</View>
</SafeAreaProvider>
);
}
export default HomeScreen;
Detail.js
import React from 'react';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {View, Text} from 'react-native';
function DetailScreen({route}) {
const { day } = route.params;
return(
<SafeAreaProvider>
<View>
<Text>{day}</Text>
</View>
</SafeAreaProvider>
);
}
export default DetailScreen;
And this is my development environment.
OS: macOS Monterey(12.6.3)
Development Program: Visual Studio Code (1.71.1)
Simulator: iPhone 14 (ios 16.2)
React native Version: 9.2.0
And I am using expo go to test it.
If anyone can help me please help.
because day value callback onDayPress of Calendar return is a object, you need parse it to string to show in screen. You can console.log(day) in file Detail.js to check pass params

ReactNative Navigation to a screen in the same direcotry

I am very new to react native and I had one question to ask. I have two screens in the same directory
screens:
Pro.js
Register.js
in Pro.js , there is one button to navigate to Register.js
<Button
textStyle={{ fontFamily: 'montserrat-regular', fontSize: 12 }}
style={styles.button}
onPress={() => navigation.navigate("Register")}>
GET STARTED
</Button>
When I run above, I get below error:
The action 'NAVIGATE' with payload {"name":"Register"} was not handled by any navigator.
Do you have a screen named 'Register'?
If you're trying to navigate to a screen in a nested navigator, see https://reactnavigation.org/docs/nesting-navigators#navigating-to-a-screen-in-a-nested-navigator.
This is a development-only warning and won't be shown in production.
Can anyone help me to navigate to that page?
Check below code:
App.js
import React from 'react';
import { createStackNavigator } from '#react-navigation/stack';
import { NavigationContainer } from '#react-navigation/native';
import RegisterScreen from './RegisterScreen';
import HomeScreen from './HomeScreen';
const Stack = createStackNavigator();
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Register" component={RegisterScreen} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
HomeScreen.js
import React from 'react';
import { View, Text, Button } from 'react-native';
const HomeScreen = ({ navigation }) => {
return (
<View>
<Text>This is the Home screen</Text>
<Button
title="Go to Register"
onPress={() => navigation.navigate('Register')}
/>
</View>
);
};
export default HomeScreen
RegisterScreen.js
import React from 'react';
import { View, Text, Button } from 'react-native';
const RegisterScreen = () => {
return (
<View>
<Text>This is the Register screen</Text>
</View>
);
};
export default RegisterScreen

Open Drawer when tapped on DrawerNavigator header button

I am trying to call openDrawer option from header of DrawerNavigation but the navigation prop does not contain openDrawer function.
import React from 'react';
import {View, TouchableOpacity} from 'react-native';
import {createDrawerNavigator} from '#react-navigation/drawer';
import Icon from 'react-native-vector-icons/Ionicons';
import {dimensions} from '../../constants/utils';
import CustomDrawerContent from './components/CustomDrawerContent';
const Drawer = createDrawerNavigator();
const DrawerNavigator = () => {
screenOptionsProps = {
screenOptions: {
headerLeft: props => (
<View>
<TouchableOpacity onPress={() => navigation.openDrawer()}>
<Icon
name="reorder-three-sharp"
size={dimensions.width * 0.08}
{...props}
/>
</TouchableOpacity>
</View>
),
},
};
return (
<Drawer.Navigator
{...screenOptionsProps}
drawerContent={props => <CustomDrawerContent {...props} />}>
<Drawer.Screen name="Dashboard" component={Dashboard} />
</Drawer.Navigator>
);
};
export default DrawerNavigator;
Whenever the Icon is being tapped drawer should be opened but navigation prop is not receiving anything and while consoling the navigation prop getting undefined as value. The props passed in drawerContent has openDrawer() method with in it but how to use it for screenOptions.
You can try "props.navigation.openDrawer()" or "props.navigation.toggleDrawer()"
headerLeft: props => (
<View>
<TouchableOpacity onPress={() =>
props.navigation.openDrawer()}>
<Icon
name="reorder-three-sharp"
size={dimensions.width * 0.08}
{...props}
/>
</TouchableOpacity>
</View>
),
Working on single time but when I return after to home screen it does not receive openDrawer() and closeDrawer() in props.navigation
For opening drawer navigation, finally i found the solution in the navigation props reference https://reactnavigation.org/docs/navigation-prop/. For anybody who got stuck with similar problem, it might be helpful.
So for the above problem i used useNavigation hook from #react-navigation/native which provides navigation object which can be further used for calling the openDrawer() method.
import React from 'react';
import {View, TouchableOpacity} from 'react-native';
import {createDrawerNavigator} from '#react-navigation/drawer';
import Icon from 'react-native-vector-icons/Ionicons';
import {dimensions} from '../../constants/utils';
import { DrawerActions, useNavigation } from "#react-navigation/native";
import CustomDrawerContent from './components/CustomDrawerContent';
const Drawer = createDrawerNavigator();
const DrawerNavigator = () => {
const navigation = useNavigation();
screenOptionsProps = {
screenOptions: {
headerLeft: props => (
<View>
<TouchableOpacity onPress={() => navigation.dispatch(DrawerActions.openDrawer())}>
<Icon
name="reorder-three-sharp"
size={dimensions.width * 0.08}
{...props}
/>
</TouchableOpacity>
</View>
),
},
};
return (
<Drawer.Navigator
{...screenOptionsProps}
drawerContent={props => <CustomDrawerContent {...props} />}>
<Drawer.Screen name="Dashboard" component={Dashboard} />
</Drawer.Navigator>
);
};
export default DrawerNavigator;

How to open drawer in react native using navigation.openDrawer()

I wanted to create a burger icon to open drawer but has been unable to do so. I have included the icon button on the right of my header. Whenever I click it to open the drawer, it shows the error of undefined function. I have mentioned the error below.
TypeError: navigation.openDrawer is not a function. (In 'navigation.openDrawer()', 'navigation.openDrawer' is undefined)
This is App.js, in this I have mentioned the code for drawer using createDrawerNavigator()
App.js
import React from 'react';
import { NavigationContainer } from '#react-navigation/native';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import PassengerScreen from './Passenger';
import Lovedones from './Lovedones';
import NewTab from './FirstScreen';
import { createDrawerNavigator } from '#react-navigation/drawer';
const Drawer = createDrawerNavigator();
export default App=() => (
<NavigationContainer>
<Drawer.Navigator initialRouteName="Passenger" drawerPosition="right" >
<Drawer.Screen name="Passenger" component={PassengerScreen} options= {{ title:'Passenger'}} />
<Drawer.Screen name="Lovedones" component={Lovedones} options= {{ title:'loved ones!!'}} />
<Drawer.Screen name="New Screen" component={NewTab} />
</Drawer.Navigator>
</NavigationContainer>
);
This is AccountScreen.js, in this code, I have created the icon button to open the drawer using navigation.openDrawer()
AccountScreen.js
import * as React from 'react';
import { View, Text, StyleSheet, Button } from 'react-native';
import { createStackNavigator } from '#react-navigation/stack';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import Ionicons from 'react-native-vector-icons/Ionicons';
import { createDrawerNavigator } from '#react-navigation/drawer';
const Stack= createStackNavigator();
Account = () => {
return <View style={styles.container}>
<Text>Hello!!</Text>
</View>
};
export default AccountStack =(navigation)=>(
<Stack.Navigator >
<Stack.Screen name="Account" component= {Account} options={{headerRight: () => (<Ionicons.Button name="reorder-three" color={"#FF0000"} size={40} backgroundColor= {"none"} onPress={() => navigation.openDrawer()}/> ) }} />
</Stack.Navigator>
);
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
Kindly help me in identifying the error.
The problem is in this line
export default AccountStack =(navigation)=>.
Here your navigation variable points to the props instead of props.navigation.
To fix this you should destructure your navigation object from the props and change this line to
export default AccountStack =({navigation})=>

TouchableRipple onPress is not working with react navigation

I am using stack navigator in my app to navigate through different components. but here I am facing 2 different issues. issue (1) I used a switch to toggle between dark and light theme, in the module, it works fine when using onPress but when called through stack screen it is not working. issue (2) I wanted to give some space between header text and the switch button but every time I add some padding to switch component, it is also applied to the header text. How can I solve these issues?
Here is App.js Component code:
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* #format
* #flow strict-local
*/
import React from 'react';
import {StatusBar, View} from 'react-native';
import {NavigationContainer} from '#react-navigation/native';
import Home from './src/screens/Home';
import Details from './src/screens/Details';
import {createStackNavigator} from '#react-navigation/stack';
import {DarkTheme, Text, Title} from 'react-native-paper';
const Stack = createStackNavigator();
const App = () => {
const Darkthemebtntoggle = () => {
return (
<Details />
);
};
return (
<>
<NavigationContainer>
<StatusBar barStyle="dark-content" />
<Stack.Navigator screenOptions={{headerTitleAlign: ''}}>
<Stack.Screen
name="Home"
component={Home}
options={{
headerTitle: (props) => (
<Darkthemebtntoggle {...props} />
),
}}
/>
</Stack.Navigator>
</NavigationContainer>
</>
);
};
export default App;
Here is also Details.js component code:
import React, {useState} from 'react';
import {View, Text} from 'react-native';
import {TouchableRipple, Switch, Title} from 'react-native-paper';
function Details() {
const [isDartheme, setDarkTheme] = useState(false);
const togglemethod = () => {
setDarkTheme(!isDartheme);
};
return (
<View style={{flexDirection:"row"}}>
<View >
<Title>
<Text >Home</Text>
</Title>
</View>
<View style={{ paddingLeft:10, flexDirection:"row"}}>
<TouchableRipple
onPress={() => {
togglemethod();
}}>
<View pointerEvents="none">
<Switch value={isDartheme} />
</View>
</TouchableRipple>
</View>
</View>
);
}
export default Details;
Here is a screenshot of the behavior of the header: