简体   繁体   English

undefined is not an object (evalating 'this.props.navigation.navigate') error in react native

[英]undefined is not an object (evaluating 'this.props.navigation.navigate') error in react native

I read the some of the solutions related to this error, but didn't made use of that.我阅读了与此错误相关的一些解决方案,但没有使用它。 Most of them are getting same error because of different reasons.由于不同的原因,他们中的大多数人都会遇到相同的错误。 I am kind of beginner to React-Native.我是 React-Native 的初学者。 So please help me out!所以请帮帮我!

App.js应用程序.js

import loginScreen from './components/Login';
import React from 'react'
import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer} from 'react-navigation';
import homeScreen from "./HomeScreen";
import LoadingScreen from "./components/LoadingScreen";
import SignUpScreen from "./components/SignUpScreen";
import StarterScreen from "./components/starter";
import ShoppingCartIconScreen from './components/ShoppingCartIcon';
import SummeryScreen from './components/summery'

const MainNavigator=createStackNavigator(
    {
      signup:{screen:SignUpScreen},
      login:{screen:loginScreen},
      Loading: {screen: LoadingScreen },
      Summery:{screen:SummeryScreen}, //exporting the Summery component
      Starter:{screen:StarterScreen,
      navigationOptions:{
        title:'Starters',
        headerRight:<ShoppingCartIconScreen/>,//using as a icon on the navigation header.
        headerStyle:{
          backgroundColor:'#694fad'
        },
        headerTitleStyle:{
          color:'white'
        }
      }},
      Home: { screen: homeScreen,
      navigationOptions:{
        headerTitle:'Home',
        headerRight:<ShoppingCartIconScreen/>,
        headerStyle:{
          backgroundColor:'#694fad',
        },
        headerTitleStyle:{
          color:'white'
        }
      }}
      },
    {
      initialRouteName:"Home"  //set home as a default screen
    }
  );
  const App=createAppContainer(MainNavigator);
  export default App; //exporting App.js with stack navigator

ShoppingCartIcon.js购物车图标.js

import React from 'react';
import {View,Text,StyleSheet,Platform} from 'react-native';
import Icon from '@expo/vector-icons/Ionicons'

//creating a constant
const ShoppingCartIcon = (props) => (
        <View style={[{ padding: 5 }, Platform.OS == 'android' ? styles.iconContainer : null]}>
        <View style={{
            position: 'absolute', height: 20, width: 20, borderRadius: 15, backgroundColor: '#e3e3e3', right: 6, bottom: 29, alignItems: 'center', justifyContent: 'center', zIndex: 2500,

        }}>
            <Text style={{ color: 'black', fontWeight: 'bold' }}>0</Text>
        </View>
        <View style={{top:3}}>
        <Icon onPress={()=>props.navigation.navigate("Summery")}  name="ios-cart" size={35} color='yellow' /> //navigate to summery screen on icon press
        </View>
    </View>
)

export default ShoppingCartIcon; //exporting shoppingcarticon

Summery.js //just a dummy file import React from 'react' import {View,Text,StyleSheet} from 'react-native' Summery.js //只是一个虚拟文件 import React from 'react' import {View,Text,StyleSheet} from 'react-native'

//nothing special here
export default class summery extends React.Component {
    render() {
        return (
            <View style={styles.container}>
            <Text>summery page</Text>
            </View>
        )
    }
}

this.props.navigation is only available in Components that are directly assigned as screens in a navigator, such as the stack navigator generated by createStackNavigator . this.props.navigation仅在导航器中直接分配为屏幕的组件中可用,例如由createStackNavigator生成的堆栈导航器。

If you want access to navigation outside of those Components, either directly pass the navigation prop (which I would not recommend), or follow this tutorial to navigate without the navigation prop: https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html如果您想访问这些组件之外的导航,请直接传递navigation道具(我不推荐),或者按照本教程在没有navigation道具的情况下进行导航: https://reactnavigation.org/docs/en/navigating-没有导航-prop.html

Well, I think your error is because you didn't add ShoppingCartIcon as part of your StackNavigator, for this reason, you cannot query navigator properties from props.好吧,我认为您的错误是因为您没有将 ShoppingCartIcon 添加为 StackNavigator 的一部分,因此,您无法从道具中查询导航器属性。 What you can do is pass navigator properties of App through props of ShoppingCartIcon, I mean you should write something like this你可以做的是通过 ShoppingCartIcon 的道具传递 App 的导航器属性,我的意思是你应该写这样的东西

// App.js
import LoginScreen from './components/Login';
import React from 'react'
import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer} from 'react-navigation';
import HomeScreen from "./HomeScreen";
import LoadingScreen from "./components/LoadingScreen";
import SignUpScreen from "./components/SignUpScreen";
import StarterScreen from "./components/starter";
import ShoppingCartIconScreen from './components/ShoppingCartIcon';
import SummeryScreen from './components/summery'

const MainNavigator=createStackNavigator(
    {
      Signup:SignUpScreen,
      Login:LoginScreen, //Name of components have to start with Uppercase Letter
      Loading: LoadingScreen,
      Summery: SummeryScreen, //exporting the Summery component
      Starter:StarterScreen,
      Home: HomeScreen,
    },
    {
      initialRouteName:"Home"  //set home as a default screen
    }
  );
  const AppContainer=createAppContainer(MainNavigator);
  class App extends React.Component{
      constructor(props){
          super(props)
      }
      render(){
          return(
            <AppContainer/>
          )
      }
  }
  export default App; //exporting App.js with stack navigator


  //ShoppingCartIcon.js
import React from 'react';
import {View,Text,StyleSheet,Platform} from 'react-native';
import Icon from '@expo/vector-icons/Ionicons'

//creating a constant
const ShoppingCartIcon = (navigation) => (
        <View style={[{ padding: 5 }, Platform.OS == 'android' ? styles.iconContainer : null]}>
        <View style={{
            position: 'absolute', height: 20, width: 20, borderRadius: 15, backgroundColor: '#e3e3e3', right: 6, bottom: 29, alignItems: 'center', justifyContent: 'center', zIndex: 2500,

        }}>
            <Text style={{ color: 'black', fontWeight: 'bold' }}>0</Text>
        </View>
        <View style={{top:3}}>
        <Icon onPress={()=> navigation.navigate("Summery")}  name="ios-cart" size={35} color='yellow' /> //navigate to summery screen on icon press
        </View>
    </View>
)

export default ShoppingCartIcon; //exporting shoppingcarticon

//Home.js or Starter.js
class Name extends React.Component{
    //Add this
    static navigationOptions = ({ navigation }) => { //Configuración de pantalla
        return {
            title:'Name',
            headerRight:(<ShoppingCartIconScreen navigation = {navigation}/>),//using as a icon on the navigation header.
            headerStyle:{
              backgroundColor:'#694fad'
            },
            headerTitleStyle:{
              color:'white'
            }
        };
      };
}

It´sa better approach to define navigationOptions for every single screen in this case.在这种情况下,为每个屏幕定义 navigationOptions 是一种更好的方法。 Just like I describe on the code above.就像我在上面的代码中描述的那样。 Furthermore, I advise to use this approach to define react components:此外,我建议使用这种方法来定义反应组件:

class App extends React.Component{
      constructor(props){
          super(props)
      }
      //Some functions ...
      render(){ return(//.... what you want)}

I hope this helps you:)我希望这可以帮助你:)

暂无
暂无

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

相关问题 未定义不是对象(评估&#39;this.props.navigation.navigate&#39;)React native - Undefined is not an object (evaluating 'this.props.navigation.navigate') React native undefined 不是一个对象(评估&#39;this.props.navigation.navigate&#39;)-React Native - undefined is not an object (evaluating 'this.props.navigation.navigate') - React Native 错误未定义不是对象(评估&#39;this.props.navigation.navigate&#39;)// React-native - Error undefined is not an object (evaluating 'this.props.navigation.navigate') // React-native 反应原生this.props.navigation.navigate未定义 - React native this.props.navigation.navigate undefined React Native:undefined 不是对象(评估“props.navigation.navigate”) - React Native: undefined is not an object (evaluating 'props.navigation.navigate') React Native错误:undefined不是对象(评估&#39;_this2.props.navigator.push&#39;) - React Native error: undefined is not an object (evaluating '_this2.props.navigator.push') React Native-“未定义不是对象对native进行评估this.props.navigation” - React Native- “undefined is not an object react native evaluating this.props.navigation” 当我尝试从一个页面导航到另一页面时,undefined不是对象(评估&#39;_this.props.navigator.push&#39;)是本机的 - undefined is not an object (evaluating '_this.props.navigator.push') react native , when i am trying to navigate from one page to another React Native,Android,未定义不是对象(评估&#39;this.props.navigator.push&#39;) - React Native, Android, undefined is not an object (evaluating 'this.props.navigator.push') react-native:未定义不是对象(评估&#39;_this.props.navigator.push&#39;) - react-native: undefined is not an object (evaluating '_this.props.navigator.push')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM