尝试实施 React Native Navigation 时出现错误 "createStackNavigator is not a function"

Getting error "createStackNavigator is not a function" when trying to implement React Native Navigation

我一直在尝试通过学习本教程系列来学习 React Native,但一直坚持使用 React Native 导航。 https://www.youtube.com/watch?v=5uIftiPLsC4&list=PLYxzS__5yYQlHANFLwcsSzt3elIbYTG1h&index=21

在 iPhone 模拟器上,我收到此错误:

(0, _reactNavigation.createStackNavigator) is not a function. (In '(0, _reactNavigation.createStackNavigator)({
    home: App,
    test: Test
})', '(0, _reactNavigation.createStackNavigator)' is undefined)

在 Android 上,我收到此错误:

Properties can only be defined on Objects.

这是我在 App.js

中的代码

import React, {Component} from 'react';
import {
  Platform, 
  StyleSheet, 
  Text, 
  View,
  Button
} from 'react-native';
import {createStackNavigator} from 'react-navigation';


class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          This is App component!
        </Text>
        <Button onPress={() => this.props.navigation.navigate('test')} title="Go to Test"></Button>
      </View>
    );
  }
}

class Test extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          This is Test component!
        </Text>
        <Button onPress={() => this.props.navigation.navigate('home')} title="Go to App"></Button>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

export default createStackNavigator({
  home: App,
  test: Test
})

它基本上只是从教程中复制的,但我可以让它显示任何内容的唯一方法是从底部删除以下内容

export default createStackNavigator({
  home: App,
  test: Test
})

并将导出默认添加回 App,但显然导航将无法正常工作。

我已经安装了 react-navigation 和 react-native-gesture-handler(也有链接)并按照文档中的说明将这些行添加到 android MainActivity.java。

我错过了什么吗?提前致谢。

如果你是 react-navigation v-3 那么你必须添加 createAppContainer

像这样。有用。在这里打卡https://snack.expo.io/@masukhelal/navigation-example

import React, {Component} from 'react';
import {
  Platform, 
  StyleSheet, 
  Text, 
  View,
  Button
} from 'react-native';
import { createAppContainer, createStackNavigator } from 'react-navigation';


class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          This is App component!
        </Text>
        <Button onPress={() => this.props.navigation.navigate('test')} title="Go to Test"></Button>
      </View>
    );
  }
}

class Test extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          This is Test component!
        </Text>
        <Button onPress={() => this.props.navigation.navigate('home')} title="Go to App"></Button>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

const AppNavigator = createStackNavigator({
  home: App,
  test: Test
})

const AppContainer = createAppContainer(AppNavigator);

export default AppContainer;