如何解决react native navigation. navigation错误

fgw7neuy  于 2023-03-31  发布在  React
关注(0)|答案(3)|浏览(227)

这是我第一次使用react native,我想移动到另一个屏幕(slide2),我有一个错误,上面写着“Undefined is not a object(evaluating navigation.navigate)”我有点卡住了,这是我已经走了多远。请也解释一下,我会非常感激,并告诉我要添加什么和在哪里,谢谢

slideOne.js页面代码

import 'react-native-gesture-handler';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import React from 'react';
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({navigation}) {
  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;
z8dt9xmd

z8dt9xmd1#

您的index.js文件不正确,请将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 {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);

您应该将App注册为AppRegistry组件,而不是注册SlideOne

rm5edbpk

rm5edbpk2#

你没有初始化你的“导航”常量。试试这个:- 从@react-navigation/native导入useNavigation

import { useNavigation } from '@react-navigation/native';
  • 像这样初始化你的导航常量

const navigation = useNavigation();

ni65a41a

ni65a41a3#

我不知道为什么会发生这种情况,尽管我们正在遵循官方文件。
我希望下面的解决方案将为您工作,因为它也是为我工作。

npm i @react-native-community/masked-view @react-navigation/native @react-navigation/stack react-native-gesture-handler react-native-safe-area-context react-native-screens@3.2.0 react-navigation

然后删除node_modules然后再次安装node_modules使用

npm i

相关问题