Drawer menu not closing - react-native

I created a drawer menu using the DrawerNavigation feature of react-navigation. I wanted to create a button that would close the menu but the onPress function seems to not work
I've imported DrawerActions from 'react-navigation-drawer' and I've tried using different syntaxes
ex.) this.props.navigation.dispatch(DrawerActions.closeDrawer());
this.props.navigation.closeDrawer();
import React, { Component } from 'react';
import {
Image,
StyleSheet,
Text,
ImageBackground,
TouchableWithoutFeedback,
View,
Button,
ScrollView,
} from 'react-native';
import {
createDrawerNavigator,
createStackNavigator,
createAppContainer,
DrawerItems,
SafeAreaView,
NavigationActions
} from 'react-navigation';
import { DrawerActions, } from 'react-navigation-drawer';
const navigator = createDrawerNavigator(
{
Home: Lander,
Page1: Lander,
Page2: Lander,
Page3: Lander,
Page4: Lander,
},
{
contentComponent: (props) => (
<SafeAreaView>
<View style= {{backgroundColor:'black'}}>
<TouchableWithoutFeedback onPress={() => this.props.navigation.dispatch(DrawerActions.closeDrawer())}>
<Image source={require('./Images/x.png')} style = {styles.cross}/>
</TouchableWithoutFeedback>
</View>
<ScrollView style= {{backgroundColor: 'black', paddingLeft: '5%'}}>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
)
},
);
I ultimately want to be able to click on the x button and let it redirect me to the home screen.
I'm getting the following error
Undefined is not an object (evaluating '_this.props.navigation')

You are importing DrawerActions from the wrong package.
Change
import { DrawerActions, } from 'react-navigation-drawer';
To
import { DrawerActions } from 'react-navigation'
And to close it, you do it like
onPress={() => this.props.navigation.dispatch(DrawerActions.openDrawer())}
Edit:
What you are doing wrong is that you get this.props but it's only props.
contentComponent: (props) => (
<SafeAreaView>
<View style= {{backgroundColor:'black'}}>
{// changed to props without this}
<TouchableWithoutFeedback onPress={() => props.navigation.dispatch(DrawerActions.closeDrawer())}>
<Image source={require('./Images/x.png')} style = {styles.cross}/>
</TouchableWithoutFeedback>
</View>
<ScrollView style= {{backgroundColor: 'black', paddingLeft: '5%'}}>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
)

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));
hope is helps

Related

Why can I not navigate to another page from FlatList component in react native?

I'm not sure what's wrong here, as far as I can tell I am doing this correctly as it has worked everywhere else in my app. The only difference is that I'm trying to do this from a FlatList component. What is wrong with this? I've also tried putting {navigation} in renderMyItem instead of FLToyCard brackets, didn't work either. Error I'm getting is:
TypeError: undefined is not an object (evaluating 'navigation.navigate').
import { StyleSheet, Text, View, FlatList } from 'react-native'
import React from 'react'
import Toy from './Database'
import ToyCard from './ToyCard'
const FLToyCard = ({navigation}) => {
const headerComp = () => {
return(
<View style={{alignSelf: 'center'}}>
<Text style={{fontSize: 25, padding: 10}}>All Toys For Sale</Text>
</View>
)
}
const renderMyItem = ({item}) => {
return(
<View style={{flex: 1}}>
<ToyCard
name={item.name}
image={item.image}
price={item.price}
desc={item.desc}
seller={item.seller}
onPress={()=>navigation.navigate('SlugProduct')}
/>
</View>
)
}
return(
<View>
<FlatList
data={Toy}
renderItem={renderMyItem}
keyExtractor={(item)=>item.id}
numColumns={2}
ListHeaderComponent={headerComp}
/>
</View>
)
}
export default FLToyCard
This is my App.js:
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
import { NavigationContainer } from '#react-navigation/native'
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import SlugProduct from './Screens/SlugProduct';
export default function App() {
const Stack = createNativeStackNavigator()
return (
<NavigationContainer>
<Stack.Navigator initialRouteName='Login' >
// list of other pages
<Stack.Screen name="SlugProduct" component={SlugProduct} />
<Stack.Screen name='ViewToys' component={ViewToys}
</Stack.Navigator>
</NavigationContainer>
);
}
EDIT: heres the ViewToys page that FLToyCard is being input into. I've tried adding the onPress into both Views and FLToyCard but got the same error each time.
import { StyleSheet, Text, View, ScrollView} from 'react-native'
import React from 'react'
import FLToyCard from '../Components/FlatListCards'
const ViewToys = ({navigation}) => {
return (
<View style={{backgroundColor: '#ffce20', height: '100%'}}>
<View>
<FLToyCard />
</View>
</View>
)
}
export default ViewToys
The issue is that your not passing navigation props from screen ViewToys into component FLToyCard.
Try this
In ViewToys Screen:
import { StyleSheet, Text, View, ScrollView} from 'react-native'
import React from 'react'
import FLToyCard from '../Components/FlatListCards'
const ViewToys = ({navigation}) => {
return (
<View style={{backgroundColor: '#ffce20', height: '100%'}}>
<View>
<FLToyCard navigation={navigation}/>
</View>
</View>
)
}
export default ViewToys
change this <FLToyCard /> to this <FLToyCard navigation={navigation} />
and you are all done.
you are getting undefined in navigation in FLToyCard component that's why you are getting this error.
This error come when we do not put these mention in a page
import { NavigationContainer,useFocusEffect } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
So I think put above in every page once your problem will solve,

I'm not able to navigate to a screen

Home.js
this is the code for my Home.js screen
import React from "react";
import { StyleSheet, Text, View, Image, FlatList, TouchableOpacity, ImageBackground } from "react-native";
import colors from "../assets/Colors/colors";
import { ScrollView } from "react-native";
import { Entypo } from '#expo/vector-icons';
import activitiesData from "../assets/Data/activitiesData";
import discoverCategoriesData from "../assets/Data/discoverCategoriesData";
import discoverData from "../assets/Data/discoverData";
import learnMoredata from "../assets/Data/learnMoredata";
import { SafeAreaView } from "react-native-safe-area-context";
import Profile from "../assets/Images/person.png";
import { MaterialIcons } from '#expo/vector-icons';
import Details from "./Details";
import {
Lato_100Thin,
Lato_100Thin_Italic,
Lato_300Light,
Lato_300Light_Italic,
Lato_400Regular,
Lato_400Regular_Italic,
Lato_700Bold,
Lato_700Bold_Italic,
Lato_900Black,
Lato_900Black_Italic
} from '#expo-google-fonts/lato';
import { useFonts } from "#expo-google-fonts/lato";
import AppLoading from "expo-app-loading";
const Home = ({ navigation }) => {
const renderDiscoverItem = ({item}) => {
return (
<TouchableOpacity
onPress={() =>
navigation.navigate("Details")
}>
<ImageBackground
source={item.image}
style={[
styles.disoverItem,
{ marginLeft: item.id === "discover-1" ? 20 : 0 },
]}
imageStyle={styles.disoverItemImage}
>
<Text style={styles.discoverItemTitle}>{item.title}</Text>
<View style={styles.discoverItemLocationWrapper}>
<MaterialIcons name="location-pin" size={24} color="white" />
<Text style={styles.discoverItemLocationText}>{item.location}</Text>
</View>
</ImageBackground>
</TouchableOpacity>
)
}
Details.js
this is the code for my Details.js screen
import React from "react";
import { Text, View } from "react-native";
const Details = () => {
return (
<View>
<Text>
Details
</Text>
</View>
);
}
export default Details;
this is the code for my home and details screen I need to tap on the box then I should redirect on to the details screen
Look at this image
can you help me resolve this issue fast I need to complete my project
update this in your app.js
<NavigationContainer>
<AppStack.Navigator>
<AppStack.Screen name='Details' component={Details} />
</AppStack.Navigator>
</NavigationContainer>

how to go to another page in react native?

how to move from one page to another in react native, please help me I am new to react-native I want to move from this page to another by clicking a button but I don't know how to do this
this is splash screen JSON file
import React from 'react';
import { View, Text, Button, Dimensions, StyleSheet, Image, TouchableOpacity} from 'react-native';
//import { LinearGradient } from 'react-native-svg';
//import {LinearGradient} from 'react-native-linear-gradient';
//import { LinearGradient } from "react-native-svg";
//import {MaterialIcons} from 'react-native-vector-icons';
import * as Animatable from 'react-native-animatable';
//import { NavigationContainer } from '#react-navigation/native';
//import SignInScreen from './SignInScreen';
const SplashScreen = ({navigation}) => {
return (
<View style={styles.container}>
<View style={styles.header}>
<Animatable.Image animation="bounceIn" duraton="1500" source=
{require('../components/images/roz.jpg')} style={styles.logo} resizeMode='stretch'/>
</View>
<Animatable.View animation="fadeInUpBig" style={styles.footer}>
<Text style={styles.title}>Stay connected with everyone!</Text>
<Text style={styles.text}>Sign in with account</Text>
<View style={styles.button}>
<TouchableOpacity onPress={() => navigation.navigate('SignInScreen')} >
<Text style={styles.textSign}>Get Started</Text>
</TouchableOpacity>
</View>
</Animatable.View>
</View>
);
};
export default SplashScreen;
const {height} = Dimensions.get("screen");
const height_logo = height * 0.28;
you have to implement React Navigation library as described on official page
Navigating to a new screen
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details')}
/>
</View>
);
}

recieving an error 'undefined is not an object evaluating this.props.navigation' while trying to navigate to another screen

returning this error while tying to navigate using switchnavigation.
i have tried removing this. props then returns undefined 'navigation'.
import React from 'react';
import { Text, View, Image, TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import { createSwitchNavigator, createAppContainer , withNavigation } from 'react-navigation';
import {widthPercentageToDP as wp, heightPercentageToDP as hp} from 'react-native-responsive-screen';
import layout from '../../constants/LayoutStyle'
import QuoteScreen from './QuoteScreen';
const HomeScreen = () => {
return (
<View style={styles.viewStyles}>
<View style={{position: 'absolute',top: hp('50%')+wp('37.5%'),left:wp('15%'),width: wp('32.5%'),height:
wp('32.5%'),backgroundColor:'rgba(255,255,255,0.1)'}}>
<View style={{alignItems: 'center',justifyContent: 'center',flex:1}}>
<Icon name="ios-book" color="purple" size={wp('10%')}
onPress={() => this.props.navigation.navigate('Quote')}
/>
<Text style={styles.tabTextStyle}>Books</Text>
</View>
</View>
);
};
const RootStack = createSwitchNavigator(
{
Home: HomeScreen,
Quote: QuoteScreen,
}
);
const AppContainer = createAppContainer(RootStack);
export default class app extends React.Component {
render() {
return <AppContainer />
}
}
expected to complete navigation properly
HomeScreen is a functional component and hence you should not use this.props.navigation, just say props.navigation.
And If you want to use props inside the function component, then use should pass props as an argument to that functional component. Like this =>
const HomeScreen = (props) => {
return (
<View style={styles.viewStyles}>
<View style={{position: 'absolute',top:
hp('50%')+wp('37.5%'),left:wp('15%'),width: wp('32.5%'),height:
wp('32.5%'),backgroundColor:'rgba(255,255,255,0.1)'}}>
<View style={{alignItems: 'center',justifyContent: 'center',flex:1}}>
<Icon name="ios-book" color="purple" size={wp('10%')}
onPress={() => this.props.navigation.navigate('Quote')}
/>
<Text style={styles.tabTextStyle}>Books</Text>
</View>
</View>
);
};
If this does not work then pass navigation as props to HomeScreen component wherever u use, Like this =>
<HomeScreen
navigation = {this.props.navigation} // or navigation = {props.navigation}, if it is functional component
/>

React Navigation not working

I'm using Drawer Navigator from React Navigation but after creating the custom component, the navigation doesn't work. There is no issue regarding importing.
Here is the Code:-
import React, { Component } from 'react';
import {DrawerNavigator} from 'react-navigation';
import {StyleSheet,Text,View,ScrollView,Image,Dimensions,TouchableOpacity} from 'react-native';
import Ionicons from 'react-native-vector-icons/Ionicons';
import HomeScreen from './components/HomeScreen/HomeScreen';
import Home from './components/Home/Home';
import Sample from './Sample';
const{height,width}=Dimensions.get('window');
const CustomDrawer = (props) => {
return(
<View>
<View style={styles.list}>
<TouchableOpacity onPress={()=>this.props.navigation.navigate('Sample')}>
<View style={styles.listElements}>
<Ionicons name="md-home" size={25} color={'black'} style={styles.listIcons} />
<Text style={styles.listText}>Home</Text>
</View>
</TouchableOpacity>
</View>
</View>
)
}
const Drawer = DrawerNavigator({
HomeScreen: {
screen: HomeScreen
},
Home:{
screen:Home,
},
Sample:{
screen:Sample,
}
},
{
drawerWidth: 350,
contentComponent:CustomDrawer
});
export default class App1 extends React.Component {
render() {
return <Drawer />;
}
}
It throws an "Undefined is not an object (Evaluating '_this.props.navigation')" error clicking the view.