React Native:undefined 不是对象(评估“props.navigation.navigate”)

切石机

我正在学习 React Native 并尝试使用 Expo 运行此代码,它给了我异常错误:undefined is not an object(评估“props.navigation.navigate”),在 App.js 的第 17 行(在注释中标记) )

有两个文件:App.js 和 Untitled1.js

应用程序:

import { StyleSheet, Text, View, TextInput, TouchableOpacity } from 'react-native';
import 'react-native-gesture-handler';
import { NavigationContainer } from '@react-navigation/native';

  function App(props) {
    return (

      <View style={styles.container}>
        <Text style={styles.logo}>APPetito</Text>

        <Text style={styles.loginText}>Welcome to our app!</Text>
        <Text style={styles.loginText}>Choose what do you want to do</Text>

// [ THE FOLLOWING LINE CONTAINS THE ERROR ]
        <TouchableOpacity onPress={() => props.navigation.navigate("Untitled1")} style={styles.loginBtn}>

          <Text style={styles.loginText}>I eat food</Text>
        </TouchableOpacity>
        <TouchableOpacity style={styles.loginBtn}>
          <Text style={styles.loginText}>I sell food</Text>
        </TouchableOpacity>

      </View>
    );

  }
// here there are the const styles
export default App;

无题.js

import * as React from 'react';
import React, { Component } from "react";
import { StyleSheet, View, Text } from "react-native";
import Icon from "react-native-vector-icons/Entypo";
import { NavigationContainer } from '@react-navigation/native';

function Untitled1(props) {
  return (
    <View style={styles.container}>
      <Icon name="check" style={styles.icon}></Icon>
      <Text style={styles.itWorks}>It works!</Text>
    </View>
  );
}

// here there are the const styles
export default Untitled1;

我能做些什么来解决问题?

用户14361363

我认为问题在于您没有首先创建堆栈导航器来在屏幕之间导航。请参阅反应导航文档以在此处了解更多信息

根据文档,您必须实现堆栈导航器,例如:

import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';

  function Home(props) {
    return (

      <View style={styles.container}>
        <Text style={styles.logo}>APPetito</Text>

        <Text style={styles.loginText}>Welcome to our app!</Text>
        <Text style={styles.loginText}>Choose what do you want to do</Text>

        <TouchableOpacity onPress={() => props.navigation.navigate("Untitled1")} style={styles.loginBtn}>

          <Text style={styles.loginText}>I eat food</Text>
        </TouchableOpacity>
        <TouchableOpacity style={styles.loginBtn}>
          <Text style={styles.loginText}>I sell food</Text>
        </TouchableOpacity>

      </View>
    );

  }


const Stack = createStackNavigator();

function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={Home} />
        <Stack.Screen name="Untitled1" component={Untitled1} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

export default App;

我已经稍微更新了您的代码以帮助您理解这个概念。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React Redux - undefined 不是评估 this.props.navigation.navigate 的对象

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

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

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

React-native:undefined不是对象(评估'_this3.props.navigation.navigate')

Undefined 不是一个对象(评估'this.props.navigation.navigate')React native

undefined不是onPress的对象(评估this.props.navigation.navigate)

react-native-navigation给出错误“未定义不是对象(正在评估'_this.props.navigation.navigate')”

React Native props.navigation.navigate不是对象

反应native:undefined不是对象(评估'_this2.props.navigation.navigate')

React Native:React Navigation StackNavigator 不工作。得到错误:“未定义不是一个对象(评估'this.props.navigation.navigate')”

未定义(评估'this.props.navigation.navigate')React Native

react-native: undefined 不是带有反应导航的对象(评估“_this2.props.navigation”)

React Native undefined不是一个对象(评估“ props.navigation.toggleDrawer”)

undefined 不是一个对象(评估'_this2.props.navigation.navigate')

可能的未处理的承诺拒绝(id:0):TypeError:undefined不是对象(评估'_this.props.navigation.navigate')

undefined不是递归组件上的对象(评估'_this2.props.navigation.navigate')

undefined 不是一个对象(评估'_this.props.navigation.navigate')点击按钮

React Native-导航问题“未定义不是对象(this.props.navigation.navigate)”

例外:undefined不是对象(评估“ navigation.navigate”)

react-native:undefined中的抽屉导航错误不是一个对象(评估'_this.props.navigation.openDrawer()')

React-Native:undefined不是对象(评估'this.props = props')

导航到另一个屏幕时,undefined不是对象(评估“ this.props.navigation.navigate”)

尝试在两个站点 React Native 之间导航时,未定义不是对象(评估“navigation.navigate”)

React Native 中的堆栈导航器。错误“未定义不是对象(评估 this.props.navigation)”

React Native: TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate') 当点击按钮时。为什么?

React Native TypeError:undefined 不是对象(评估“props.getItem”)

TypeError: undefined is not an object (evaluating 'navigation.navigate') React Native

React Native,navigation.navigate不是一个函数