开发者问题收集

React Navigation 错误(路线未定义)

2021-09-11
958

我正在尝试将 React Navigation 用于我的 React Native 项目并遵循文档。

我的代码如下所示:

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

    const Stack = createStackNavigator();

    const Home = () => {
      <Text>Home</Text>;
    };

    const Profile = () => {
      <Text>Profile</Text>;
    };

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

我将它包装在 NavigationContainer 中,因为出现错误建议这样做,然后我收到错误:路由未定义,

非常感谢您的帮助

2个回答

这样,您就可以在 页面之间切换

import React from 'react';
import { View, Text, Button } from 'react-native';
import { createStackNavigator } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';

const Stack = createStackNavigator();

const Home = ({navigation}) => {
  return(
    <View>
      <Text>Home Screen</Text>
      <Button title="Go to Profile Screen" onPress={()=> navigation.navigate('Profile') }  />
    </View>
  )
}

const Profile = ({navigation}) => {
  return(
    <View>
      <Text>Profile Screen</Text>
      <Button title="Go to Home Screen" onPress={()=> navigation.navigate('Home')} />
    </View>
  )
}


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

如有必要,您可以添加底部 标签导航 并创建更好的视图。

import React from 'react';
import { View, Text, Button } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';

const Tab = createBottomTabNavigator();

const Home = () => {
  return(
    <View>
      <Text>Home Screen</Text>
    </View>
  )
}

const Profile = () => {
  return(
    <View>
      <Text>Profile Screen</Text>
    </View>
  )
}

export default function App() {
  return (
    <NavigationContainer>
      <Tab.Navigator>
        <Tab.Screen name="Home" component={Home} />
        <Tab.Screen name="Profile" component={Profile} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}
Hamza Azman
2021-09-11

我不确定您尝试使用路由 prop 访问什么,它里面有一个名称、索引和 params(导航时传递给组件的参数,我认为您要访问的就是这些参数)对象。 这是您如何使用它的一个例子

    //The Home component
    const Home = ({navigation) => {
          return ( 
                 <TouchableOpacity onPress = {
                  () => navigation.navigate("Profile", {
                      name: "Sam"
                  }) >
                      <Text > Send me to profile with the name sam < /Text> 
                  </TouchableOpacity >
              )
        };

//The component to be navigated to which will have the name sam
const Profile = ({route) => {
    return ( 
            <Text >Hello {route.params.name} < /Text>
        )
};
Mostafa Elkaramany
2021-09-11