【问题标题】:React Native navigation package Error React.createElement: type is invalid -- expected a stringReact Native 导航包错误 React.createElement: type is invalid -- 需要一个字符串
【发布时间】:2017-08-25 11:18:47
【问题描述】:

打开应用时显示错误。

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in.

这是我创建的基本代码。查看重现的步骤:

  1. 使用命令create-react-native-app AwesomeProject 创建了本机项目
  2. 已安装npm install --save react-navigation
  3. 将以下代码从反应导航文档中粘贴到 App.js

    import React from 'react';
    import {
    AppRegistry,
    Text,
    } from 'react-native';
    import { StackNavigator } from 'react-navigation';
    
    class HomeScreen extends React.Component {
        static navigationOptions = {
            title: 'Welcome',
        };
        render() {
            return <Text>Hello, Navigation!</Text>;
        }
    }
    
    const SimpleApp = StackNavigator({
        Home: { screen: HomeScreen },
    });
    
    AppRegistry.registerComponent('SimpleApp', () => SimpleApp);
    
  4. 使用npm start运行应用并在expo app android中打开

请注意,其他文件均未编辑。

【问题讨论】:

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


    【解决方案1】:

    Expo 和标准的 React Native 项目模板在声明应用根组件的方式上略有不同。

    而在标准 React Native 项目中,您在 index.android.js 中使用 AppRegistry:

    AppRegistry.registerComponent('SimpleApp', () => SimpleApp);
    

    在 Expo 中,框架会为您注册组件,您需要做的就是从您的Main.js 中导出组件:

    export default SimpleApp;
    

    以下是修改并粘贴到 Expo Snack 的代码示例:https://snack.expo.io/S1CZnFadZ

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 2020-11-21
      • 1970-01-01
      • 2019-04-02
      • 2018-04-17
      • 2017-09-01
      • 2019-09-12
      • 1970-01-01
      • 2022-12-28
      相关资源
      最近更新 更多