2017-08-25 73 views
0

打开应用程序时显示错误。React本地导航包错误React.createElement:类型无效 - 预期为字符串

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并在博览会应用程序中打开android

请注意,没有其他文件被编辑。

回答

2

Expo和标准的React Native项目模板有一种稍微不同的声明应用根组件的方式。

而在标准阵营本地项目,您在index.android.js使用AppRegistry:

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

在世博会上,框架您注册组件,和所有你需要做的是从你的Main.js导出组件:

export default SimpleApp; 

下面是修改并粘贴到世博小吃代码示例:https://snack.expo.io/S1CZnFadZ

相关问题