React Native:我的组件未被检测为组件

React Native : my component isn't detected as a component

我开始使用 React Native 并尝试使用屏幕之间的导航。为此,我创建了一个组件导航,但是当我想使用它时,出现了这个错误:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined

问题似乎出在 AppNavigator 组件上

我试过了:

你能帮帮我吗?

App.js :

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import AppNavigator from './components/navigation';

function App() {
  return (
    <View>
      <AppNavigator />
    </View>
  );
}


/*const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    justifyContent: 'center',
  },
});*/

export default App;

Navigation.js

import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import { NavigatorContainer } from '@react-navigation/native';
import Localisation from './localisation';
import Home from './home';

const { Navigator, Screen } = createStackNavigator()

const AppNavigator = () => (
  <NavigatorContainer>
    <Navigator  initialRouteName="Home">
      <Screen options={{headerShown: false}} name="Home" component={Home} />
    </Navigator>
  </NavigatorContainer>
 
)

export default AppNavigator;

react-navigation/native 中没有名为 NavigatorContainer 的组件。该组件称为 NavigationContainer

因此,以下应该可以解决这个问题。

import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';
import Localisation from './localisation';
import Home from './home';

const { Navigator, Screen } = createStackNavigator()

const AppNavigator = () => (
  <NavigationContainer>
    <Navigator  initialRouteName="Home">
      <Screen options={{headerShown: false}} name="Home" component={Home} />
    </Navigator>
  </NavigationContainer>
 
)

export default AppNavigator;