打开应用程序时显示错误。

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.


这是我创建的基本代码。请参见复制步骤:


使用命令create-react-native-app AwesomeProject创建的本机项目
已安装npm install --save react-navigation
将以下代码从react导航文档粘贴到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);

使用npm start运行应用程序并在android expo app中打开


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

最佳答案

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

关于javascript - React Native导航包错误React.createElement:类型无效-预期为字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45880180/

10-12 02:42
查看更多