【问题标题】:I am geeting error : ReferenceError: navigation is not defined in react native navigation. I am just learning react native我收到错误:ReferenceError:导航未在反应本机导航中定义。我只是在学习 React Native
【发布时间】:2021-05-05 10:22:15
【问题描述】:

我正在尝试学习本机反应,但出现以下错误

ReferenceError:导航未定义

我是初学者,请你指导我解决这个错误。

我也尝试过改用功能组件,但得到同样的错误。

我正在使用 expo 进行学习


import 'react-native-gesture-handler';

import React, { Component } from "react";
import { ActivityIndicator, StyleSheet, Text, View, Image, Button,ImageBackground } from "react-native";

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

class HomeScreen extends Component {
  render() {
    return (
      <View style = {styles.container}>
      <Text>Hello HomeScreen</Text>
      <Button
        title="Go to Profile"
        onPress={() => navigation.navigate('Profile')}
      />

      </View>
    );
  }
}

class Profile extends Component {
  render() {
    return (
      <View style = {styles.container}>
      <Text>Hello profile</Text>

      </View>
    );
  }
}

const Stack = createStackNavigator();
class App extends Component {
  
  render() {
    return (
      <NavigationContainer>
      <Stack.Navigator initialRouteName="HomeScreen">
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Profle" component={Profile} />
      </Stack.Navigator>
    </NavigationContainer>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems : 'center'
  },
});

export default App;







【问题讨论】:

    标签: react-native navigation react-navigation


    【解决方案1】:

    您正在使用基于类的组件,因此请使用 this.props.navigation

     <Button
            title="Go to Profile"
            onPress={() => this.props.navigation.navigate('Profile')}
          />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多