Getting undefined is not an object evaluating _this.props.navigation
Clash Royale CLAN TAG#URR8PPP
Getting undefined is not an object evaluating _this.props.navigation
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 got this error:
How Can I fix That?
DrawerNavigator
Router page
mainScreen
photos page
<TouchableHighlight onPress=() => this.props.navigation.navigate('DrawerOpen')>
<TouchableHighlight onPress=() => this.props.navigation.navigate('DrawerOpen')>
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>
)
This is my all code without my style, i passed my photos page from ` <MyNavScreen navigation=navigation />` , and I can see photo text and back button in photo page, but i should add my router page if you need?
– Sedric Heidarizarei
Jul 20 '17 at 23:32
3 Answers
3
Perhaps I'm overlooking something, but it just looks like a simple Javascript error. You're destructing your props in your pure component MyNavScreen
:
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:
this.props
navigation
<TouchableHighlight onPress=() => this.props.navigation.navigate('DrawerOpen')>
If you change it instead to use navigation
directly, it should work:
navigation
<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()
.
mainScreen
this.props
render()
You should brush up on destructing if this is causing you trouble.
Thank you Dear @michael-cheng, Solved.and whitch way is better? my code is okey? and can i continue? or I should change to reder() mode and add this.props?
– Sedric Heidarizarei
Jul 21 '17 at 2:07
@SedricHeidarizarei I'm of the opinion that with destructuring, there is no "better way". It's simply a tool to help make code more readable. Whether or not you use it depends on your style and your team's style. This is my opinion though and I'm certainly not an expert on Javascript so take my word with a grain of salt.
– Michael Cheng
Jul 21 '17 at 2:39
@SedricHeidarizarei As for pure components, there are good reasons to use them. There are quite a few articles online about when and how to best use them. I recommend taking a bit of time to read up on them as it'll give you a better understanding of both React and how to architect your code. There's even a question on StackOverflow covering this topic that goes into more nuances. For example, your
photos page
specifically is a functional stateless component.– Michael Cheng
Jul 21 '17 at 2:43
photos page
Thank you @michael-cheng
– Sedric Heidarizarei
Jul 21 '17 at 5:56
See stackoverflow.com/a/49686927/2101864 for more up-to-date apprach.
– Gürol Canbek
Apr 6 at 6:47
If you use TouchableOpacity/Height in your child element, pass it this.props.onPress
like this:
this.props.onPress
<TouchableOpacity onPress=this.props.onPress/>
Then call the onPress function in your parent component like this:
<Parent onPress=this.Handlepress />
thanks. You saved me.
– sonicmario
Jun 2 at 11:56
i had the same problem when i was using the header component
now you can use the navigation variable in other component like this
<TouchableOpacity onPress=() => this.props.navigation.navigate("Play");>
Happy Codding :)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Well I think it is evident you are not including everything in the photos page. Please update your code with all of the code from both files so we can see what is missing.
– wuno
Jul 20 '17 at 23:16