简体   繁体   English

undefined 不是一个对象(评估'this.props.navigation.navigate')-React Native

[英]undefined is not an object (evaluating 'this.props.navigation.navigate') - React Native

I am trying to make my first React Native Android app and I am getting this error:我正在尝试制作我的第一个 React Native Android 应用程序,但出现此错误:

undefined is not an object (evaluating 'this.props.navigation.navigate') undefined 不是一个对象(评估'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 coming.我无法弄清楚为什么会出现错误。

You are exporting the component 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;你应该在你的class HomeScreen定义中去掉export default ,并在文件底部做export default App;

If you are handling it in AppContainer and not able to access while opening drawer menu.如果您在 AppContainer 中处理它并且无法在打开抽屉菜单时访问它。 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' })

尝试不使用“搜索词”或以这种方式:

navigate('route', {item:'search term'})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 【React-native】undefined不是对象(评估&#39;_this.props.navigation.navigate&#39;) - 【React-native】undefined is not an object (evaluating '_this.props.navigation.navigate') Undefined 不是 React Native 上的 object(评估“this.props.navigation.navigate”) - Undefined is not an object (evaluating 'this.props.navigation.navigate') on React Native 未定义(评估&#39;this.props.navigation.navigate&#39;)React Native - undefined (evaluating 'this.props.navigation.navigate') React Native React Native:React Navigation StackNavigator 不工作。 得到错误:“未定义不是一个对象(评估&#39;this.props.navigation.navigate&#39;)” - React Native: React Navigation StackNavigator not working. Getting error: “undefined is not an object (evaluating 'this.props.navigation.navigate')” react-native-navigation给出错误“未定义不是对象(评估&#39;_this.props.navigation.navigate&#39;)” - react-native-navigation giving error “undefined is not an object (evaluating '_this.props.navigation.navigate')” undefined 不是 object(评估 'this.props.navigation.navigate') - 基于函数方法的导航时 React Native - undefined is not an object (evaluating 'this.props.navigation.navigate') - React Native while navigation based on functions approach 未定义不是对象(评估this.props.navigation.navigate) - undefined is not an object (evaluating this.props.navigation.navigate) 未定义不是对象(评估&#39;_this.props.navigation.navigate&#39;) - undefined is not an object (evaluating '_this.props.navigation.navigate') React Native - 导航问题“未定义不是对象(this.props.navigation.navigate)” - React Native - navigation issue “undefined is not an object (this.props.navigation.navigate)” 反应本机导航(未定义不是对象.. this.props.navigation.navigate - React native navigation (undefined is not an object .. this.props.navigation.navigate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM