在本机反应中找不到导航对象错误

couldn't find a navigation object error in react native

这是我第一次使用 React Native,我想移动到另一个屏幕(幻灯片 2),我有一个错误显示 "We couldn't find a navigation object. Is your component inside a navigator?" 他们说错误在幻灯片中,我有点卡住了,这个是我走了多远。 也请解释一下,我将不胜感激,并告诉我要添加什么以及在哪里添加,谢谢。

slideOne.js 页面代码

import 'react-native-gesture-handler';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import React from 'react';
import {useNavigation} from '@react-navigation/native';
import SlideTwo from './SlideTwo';

import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
  TextInput,
  Button,
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

function SlideOne() {
const navigation = useNavigation();
  return (
    <React.Fragment>
      <View style={styles.body}>
        <View style={styles.wrapper}>
          <View style={styles.imageWrap}></View>

          <TextInput
            placeholder="What should we refer to you as?"
            placeholderTextColor="#03444F60"
            style={styles.textInput}
            underlineColorAndroid="transparent"
          />
        </View>
        <View style={styles.label}>
          <Text style={styles.labelText}>First Name</Text>
        </View>

        <View style={styles.textWrap}>
          <Text style={styles.text}>Back</Text>
          <Text style={styles.text}>Next</Text>
        </View>
        <Button
          title="Go to screen two"
          onPress={() => navigation.navigate('SlideTwo')}
        />
      </View>
    </React.Fragment>
  );
}
export default SlideOne;


这是我的index.js路由

    import 'react-native-gesture-handler';
    import {NavigationContainer} from '@react-navigation/native';
    import {createStackNavigator} from '@react-navigation/stack';
    import {AppRegistry} from 'react-native';
    import App from './App';
    import SlideOne from './SlideOne';
    import SlideTwo from './SlideTwo';
    import {name as appName} from './app.json';
    AppRegistry.registerComponent(appName, () => SlideOne);

app.js代码

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

import SlideOne from './SlideOne';
import SlideTwo from './SlideTwo';

// NAVIGATION
const StackNavigator = createStackNavigator();

function App() {
  <NavigationContainer>
    <StackNavigator.Navigator>
      <StackNavigator.Screen name="SlideOne" component={SlideOne} />
      <StackNavigator.Screen name="SlideTwo" component={SlideTwo} />
    </StackNavigator.Navigator>
  </NavigationContainer>;
}

export default App;

问题出在您的 index.js 中,您在其中使用注册了不需要的 SlideOne 组件。您应该像下面这样注册 App 组件。

AppRegistry.registerComponent(appName, () => App);

现在错误出在您的组件上,即 SlideOne 不在导航器内,这是真的。当您 运行 应用程序时,您会跳过应用程序并直接呈现未连接到任何导航器的 SlideOne 组件,因此当您尝试导航时,您最终会遇到错误。

当您使用 App 组件时,您会呈现导航器内的 SlideOne 组件,因此它会按预期工作。