【问题标题】:Getting error "createStackNavigator is not a function" when trying to implement React Native Navigation尝试实现 React Native Navigation 时出现错误“createStackNavigator 不是函数”
【发布时间】:2023-04-02 06:22:01
【问题描述】:

我一直在尝试通过遵循本教程系列来学习 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。

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

【问题讨论】:

    标签: javascript react-native react-native-navigation


    【解决方案1】:

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

    【讨论】:

    • 我尝试了您的建议,但错误仍然存​​在。当我安装反应导航和手势处理程序时,我看到了these warnings
    • 嘿@Joseph Mok,查看此链接snack.expo.io/@masukhelal/navigation-example。您的代码正在运行。你清除缓存试过了吗?
    • 搞定了!我意识到 react-navigation 是 1.6.1,所以我将它更新到 3.8.1。还用react-native run-android -- --reset-cache清除缓存非常感谢您的帮助!
    猜你喜欢
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2021-02-06
    相关资源
    最近更新 更多