尝试实现React Native Navigation时出现错误“ createStackNavigator不是函数”

时间:2019-04-22 12:23:33

标签: javascript react-native 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中。

我想念什么吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

如果您是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;