2017-08-25 41 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

相關問題