最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - undefined is not an object (evaluating 'this.props.navigation.navigate') - React Native - Stack Ove

programmeradmin3浏览0评论

I am trying to make my first React Native Android app and I am getting this error:

undefined is not an object (evaluating 'this.props.navigation.navigate')

This is the code:

import React from 'react';
import { StyleSheet, Text, View, Button, TextInput } from 'react-native';
import { StackNavigator } from 'react-navigation';

export default class HomeScreen extends React.Component {

  static navigationOptions = {
    title: 'Home',
  };

  render() {
    const { navigate } = this.props.navigation;    

    return (
      <View>
        <Button 
          title="Show Centers near me"
          onPress={() =>
            navigate('Results', "Search Term")
          }
          />
        <Text>or</Text>
      </View>
    );
  }
}


class ResultsScreen extends React.Component {

  static navigationOptions = {
    title: 'Results',
  };


  render() {
  const { navigate } = this.props.navigation;

    return (
      <View>
        <Text>Hi</Text>
      </View>
    );
  }
}

const App = StackNavigator({
  Home: { screen: HomeScreen },
  Results: { screen: ResultsScreen }
});

I can not figure out why the error is ing.

I am trying to make my first React Native Android app and I am getting this error:

undefined is not an object (evaluating 'this.props.navigation.navigate')

This is the code:

import React from 'react';
import { StyleSheet, Text, View, Button, TextInput } from 'react-native';
import { StackNavigator } from 'react-navigation';

export default class HomeScreen extends React.Component {

  static navigationOptions = {
    title: 'Home',
  };

  render() {
    const { navigate } = this.props.navigation;    

    return (
      <View>
        <Button 
          title="Show Centers near me"
          onPress={() =>
            navigate('Results', "Search Term")
          }
          />
        <Text>or</Text>
      </View>
    );
  }
}


class ResultsScreen extends React.Component {

  static navigationOptions = {
    title: 'Results',
  };


  render() {
  const { navigate } = this.props.navigation;

    return (
      <View>
        <Text>Hi</Text>
      </View>
    );
  }
}

const App = StackNavigator({
  Home: { screen: HomeScreen },
  Results: { screen: ResultsScreen }
});

I can not figure out why the error is ing.

Share Improve this question edited Jul 31, 2017 at 17:33 nem035 35.5k6 gold badges92 silver badges104 bronze badges asked Jul 31, 2017 at 13:50 Akshey JawaAkshey Jawa 4471 gold badge9 silver badges20 bronze badges 6
  • Try adding a constructor in your class like so: constructor(props) { super(props) } – Helder De Baere Commented Jul 31, 2017 at 13:52
  • @HelderJulesDeBaere Doesn't help! – Akshey Jawa Commented Jul 31, 2017 at 14:22
  • 1 Show more code around how you setup the StackNavigator. this.props.navigation is passed down by the navigator and if the setup is off then this error may happen. Also try searching on SO for this, this has been answered a lot. – hyb175 Commented Jul 31, 2017 at 14:37
  • @hyb175 I have added the StackNavigator setup. I have thoroughly searched SO and Google before posting this. None of the suggested solutions worked for me. – Akshey Jawa Commented Jul 31, 2017 at 17:12
  • This is still not enough information to help you. Try putting more plete setup so people can recreate this issue you have. Something to check first, is HomeScreen ponent used anywhere else besides in the StackNavigator call? – hyb175 Commented Jul 31, 2017 at 17:15
 |  Show 1 more ment

3 Answers 3

Reset to default 7

You are exporting the ponent wrong. You should get rid of the export default on your class HomeScreen definition and at the bottom of the file do export default App;

If you are handling it in AppContainer and not able to access while opening drawer menu. You can try below snippets.

const HomeStackNavigator = createStackNavigator({   Home:    { 
    screen: Home,
    navigationOptions : ({navigation}) => ({
      title: 'Home',
      headerStyle: {
          backgroundColor: "#512DA8"
      },
      headerTitleStyle: {
          color: "#fff"            
      },
      headerTintColor: "#fff",
      headerLeft: <TouchableOpacity onPress={ () => navigation.openDrawer()}> 
      <Image 
      source={require('./images/menu_burger.png')}
      style={{width: 24, height: 24, padding: 5, marginLeft: 5}}/>
      </TouchableOpacity>
    })   } }, {   initialRouteName: 'DumontHome' })

Try without 'search term' or in this way:

navigate('route', {item:'search term'})
发布评论

评论列表(0)

  1. 暂无评论