React Native 导航包错误React.createElement:类型无效——需要一个字符串
React Native navigation package Error React.createElement: type is invalid -- expected a string
打开应用程序时显示错误。
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
将以下代码粘贴到 App.js 来自 React 导航文档
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
并在 expo 应用中打开的应用程序 android
请注意 none 个其他文件已编辑。
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
打开应用程序时显示错误。
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
将以下代码粘贴到 App.js 来自 React 导航文档
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
并在 expo 应用中打开的应用程序 android
请注意 none 个其他文件已编辑。
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