how to go to another page in react native? - 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>
);
}

Related

cannot read property navigate of undefined

I am getting this error cannot read the property of navigate when try to navigate to the SignInScreen
this is the splash screen that have created
import React from 'react';
import {
View,
Text,
TouchableOpacity,
Dimensions,
StyleSheet,
StatusBar,
Image
} from 'react-native';
import * as Animatable from 'react-native-animatable';
import LinearGradient from 'react-native-linear-gradient';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import { useTheme } from '#react-navigation/native';
const SplashScreen = ({navigation}) => {
const { colors } = useTheme();
return (
<View style={styles.container}>
<StatusBar backgroundColor='#009387' barStyle="light-content"/>
<View style={styles.header}>
<Animatable.Image
animation="bounceIn"
duraton="1500"
source={require('../assets/logo.png')}
style={styles.logo}
resizeMode="stretch"
/>
</View>
<Animatable.View
style={[styles.footer, {
backgroundColor: colors.background
}]}
animation="fadeInUpBig"
>
<Text style={[styles.title, {
color: colors.text
}]}>Stay connected with everyone!</Text>
<Text style={styles.text}>Sign in with account</Text>
<View style={styles.button}>
<TouchableOpacity onPress={()=>navigation.navigate('SignInScreen')}>
<LinearGradient
colors={['#08d4c4', '#01ab9d']}
style={styles.signIn}
>
<Text style={styles.textSign}>Get Started</Text>
<MaterialIcons
name="navigate-next"
color="#fff"
size={20}
/>
</LinearGradient>
</TouchableOpacity>
</View>
</Animatable.View>
</View>
);
};
export default SplashScreen;
this is the main App.js FILE WHICH IS THE ROOT FILE
const Drawer = createDrawerNavigator();
// const [isEnabled, setIsEnabled] = React.useState(false);
export default function App() {
return (
<PaperProvider theme={PaperDarkTheme}>
<SplashScreen />
</PaperProvider>
);
}
Looks like you missed to wrap main Navigation container and then drawer this can work :
import * as React from 'react';
import { Button, View } from 'react-native';
import { createDrawerNavigator } from '#react-navigation/drawer';
import { NavigationContainer } from '#react-navigation/native';
const Drawer = createDrawerNavigator();
// const [isEnabled, setIsEnabled] = React.useState(false);
export default function App() {
return (
<PaperProvider theme={PaperDarkTheme}>
<NavigationContainer>
<Drawer.Navigator initialRouteName="splash">
<Drawer.Screen name="splash" component={SplashScreen } />
{// other Screens}
</Drawer.Navigator>
</NavigationContainer>
</PaperProvider>
);
}

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>

Drawer menu not closing

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

How to add a NavigationDrawer in react native?

I know this question has been asked before, but for my situation I can't find an answer. I am helping make an app with a friend and I am tasked with making a DrawerNavigator for one of our screens. I am using react native for this.
Here is my render function inside the class of one of my screens
render() {
return (
<Container>
<Header title='BarMate'/>
<View style={styles.container}>
<MapView
style={{ alignSelf: 'stretch', height: 750}}
region={this.state.mapRegion}
onRegionChange={this._handleMapRegionChange}
/>
</View>
</Container>
);
}
The problem is that when I try to add an icon and button to the Header, it just doesn't show up. Here is the code after I added the icon
render() {
return (
<Container>
<Header title='BarMate'>
<Left>
<Icon name="ios-menu" onPress={() =>
this.props.navigation.navigate('DrawerOpen')} />
</Left>
</Header>
<View style={styles.container}>
<MapView
style={{ alignSelf: 'stretch', height: 750}}
region={this.state.mapRegion}
onRegionChange={this._handleMapRegionChange}
/>
</View>
</Container>
);
}
However, this code does work if instead of importing Container and Header from a custom made file like this:
import { Container } from '../components/Container'
import { Header } from '../components/Header'
I import them like this:
import {Container, Header, Icon, Button, Content, Left } from 'native-base'
I'm a beginner in react native so there might just be something here I'm overlooking, but any help would be appreciated. Here is what's in the files Container and Header my friend made.
//Header file
import React from 'react';
import { View, Text, StatusBar } from 'react-native';
import { Icon, Button, Content, Left} from 'native-base'
import styles from './styles';
const Header = ({title}) => (
<View style={styles.container}>
<StatusBar translucent={false} barStyle="light-content" />
<Text style={styles.title}>{title}</Text>
</View>
);
export default Header;
//Container file
import React from 'react';
import PropTypes from 'prop-types'; // ES6
import { View } from 'react-native';
import styles from './styles';
const Container = ({ children }) => (
<View style={styles.container}>
{children}
</View>
);
Container.propTypes = {
children: PropTypes.any,
}
export default Container;

React Native: TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate')

Being a beginner in react-native, I can't figure out the problem in my code. By reading on the internet, I have an idea that I have some binding issue maybe.
So, my code starts with index.js and registers the App component over there. The app component just contains the stack navigation routes. It load the LoginScreen component (displays logo, background and name of the application) which in turn loads LoginForm component. There is no authentication on the Login button and the only thing I need is that the Menu component is loaded as I press the Login button. It is giving the TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate')
index.js
import { AppRegistry } from 'react-native';
import App from './App';
AppRegistry.registerComponent('bluebulk', () => App);
App.js
import { StackNavigator } from 'react-navigation';
import LoginScreen from './src/components/login/LoginScreen';
import Menu from './src/components/menu/Menu';
const App = StackNavigator({
Main: { screen: LoginScreen },
Menu: { screen: Menu }
});
export default App;
LoginScreen.js
import { StackNavigator } from 'react-navigation';
import React, { Component } from 'react';
import { StyleSheet, View, Text, Image } from 'react-native';
import LoginForm from './LoginForm';
class LoginScreen extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.logoContainer}>
<Image
style={styles.logo}
source={require('../../images/transparent.png')}
/>
<View style={{ flexDirection: 'row' }}>
<Text style={styles.blueTextStyle}>Blue</Text>
<Text style={styles.bulkTextStyle}>Bulk</Text>
</View>
</View>
<View style={styles.formContainer}>
<LoginForm />
</View>
</View>
);
}
}
export default LoginScreen;
LoginForm.js
import React, { Component } from 'react';
import {
StyleSheet,
TextInput,
TouchableOpacity,
Text,
View,
KeyboardAvoidingView,
Keyboard
} from 'react-native';
import { StackNavigator } from 'react-navigation';
class LoginForm extends Component {
render() {
return (
<KeyboardAvoidingView behavior='height' style={styles.container}>
<View style={{ flexDirection: 'row' }}>
<Text style={styles.textStyle}>Email:</Text>
<TextInput
style={styles.styleInput}
placeholder="user#gmail.com"
returnKeyType="next"
keyboardType="email-address"
onSubmitEditing={() => this.refs.password.focus()}
/>
</View>
<View style={{ flexDirection: 'row' }}>
<Text style={styles.textStyle}>Password:</Text>
<TextInput
ref='password'
style={styles.styleInput}
placeholder="password"
secureTextEntry
returnKeyType="go"
onSubmitEditing={Keyboard.dismiss}
/>
</View>
<TouchableOpacity
style={styles.buttonContainer}
onPress={() => this.props.navigation.navigate('Menu')} //Error here
>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
</KeyboardAvoidingView>
);
}
}
export default LoginForm;
Menu.js
import React, { Component } from 'react';
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native';
import { StackNavigator } from 'react-navigation';
class Menu extends Component {
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<View style={styles.viewContainer}>
<TouchableOpacity style={styles.buttonContainer}>
<Text style={styles.buttonText}>View Products</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.buttonContainer}>
<Text style={styles.buttonText}>View Discounts/Offers</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.buttonContainer}>
<Text style={styles.buttonText}>View Invoice History</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
export default Menu;
You need to pass navigation props lower down to your LoginForm component.
Try this: <LoginForm navigation={this.props.navigation} />
You should end up with the following result: