I'm using DrawerNavigator
and I have 3 pages: Router page
, mainScreen
and a photos page
.
I maked a header navbar area and I used This <TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>
to open Drawer menu in mainScreen and used that for photos page too, menu is ok in mainScreen but when I click <TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>
in photos page, I get the error:
undefined is not an object (evaluating '_this.props.navigation')
How can I fix that?
My photos page:
import React from 'react'; import { Button, ScrollView, View, Text, StyleSheet, TouchableHighlight } from 'react-native'; import { DrawerNavigator } from 'react-navigation'; import MaterialIcons from 'react-native-vector-icons/MaterialIcons'; import Icon from 'react-native-vector-icons/FontAwesome' const MyNavScreen = ({ navigation }) => ( <View> <View style={styles.containerNavbar}> <TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}> <Icon name="bars" size={30} color="#fff" /> </TouchableHighlight> <Text style={styles.navbarTitle}>Photos</Text> </View> <ScrollView> <View><Text>photo</Text></View> <Button onPress={() => navigation.goBack(null)} title="Go back" /> </ScrollView> </View> ); const MyPhotosHomeScreen = ({ navigation }) => ( <MyNavScreen navigation={navigation} /> ); MyPhotosHomeScreen.navigationOptions = { title: 'Photos', drawerIcon: ({ tintColor }) => ( <MaterialIcons name="photo" size={24} style={{ color: tintColor }} /> ), }; export default MyPhotosHomeScreen;
mainScreen:
export default class MainScreen extends React.Component { static navigationOptions = { drawerLabel: 'Home', drawerIcon: ({ tintColor }) => ( <MaterialIcons name="home" size={24} style={{ color: tintColor }} /> ) }; render() { return ( <View> <View style={styles.containerNavbar}> <TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}> <Icon name="bars" size={30} color="#fff" /> </TouchableHighlight> <Text style={styles.navbarTitle}>mainScreen</Text> </View> <View> <View style={styles.containerFooter}> <Text style={styles.footerTitle}>Footer</Text> </View> </View> </View> ) } }
The “TypeError: 'undefined' is not an object” error occurs when a property is accessed or a method is called on an undefined object. This error is shown only on safari browser.
The Cause. Simply, you're treating a variable like it is an object when it is not. 'undefined' means that your variable hasn't yet been defined (in this case, our object has either not been instantiated or it has been but not assigned to the variable that you are using.
<ChildComponent navigation={this.props.navigation}/>
props.navigation.navigate("ScreenName")
Perhaps I'm overlooking something, but it just looks like a simple Javascript error. You're destructing your props in your pure component MyNavScreen
:
const MyNavScreen = ({ navigation }) => (
This means that you don't have access to this.props
. You just have access to the destructured prop navigation
. Hence the reason for the undefined error as this really is undefined:
<TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>
If you change it instead to use navigation
directly, it should work:
<TouchableHighlight onPress={() => navigation.navigate('DrawerOpen')}>
On mainScreen
, you are fine because it's not a pure component with destructured arguments. So you still have access to this.props
in render()
.
You should brush up on destructing if this is causing you trouble.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With