【发布时间】:2017-10-13 13:02:43
【问题描述】:
我是 React-Native 的新手,并尝试按照初学者的教程开始使用它。我按照教程创建了一个“Header”组件并尝试导出和导入它并最终使用它。不幸的是,我收到一个错误:未捕获的错误:不变违规:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数,但得到:对象。 我遇到了这两个问题:
- Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object
- Uncaught Error: Invariant Violation: Element type is invalid: expected a string
但他们都没有帮助我解决我的问题。
这些是我的文件及其内容:
index.js
import { AppRegistry } from 'react-native';
import App from './App';
AppRegistry.registerComponent('albums', () => App);
App.android.js
// Import section
import React from 'react';
import { AppRegistry } from 'react-native';
import Header from './src/components/header';
// create a component
const App = () => (
<Header />
);
// Render it to the device
AppRegistry.registerComponent('albums', () => App);
header.js
// Import labraries for making a component
import React from 'react';
import { Text } from 'react-native';
// Make a component
const Header = () => {
return <Text>Albums</Text>
};
// Make the component available to other parts of the app
export default Header;
我也尝试过像这样导入标题(但没有帮助):
import { Header } from './src/components/header';
对于解决此问题的任何帮助,我将不胜感激。 在此先感谢:)
附言 我正在使用 React-Native 版本 0.49.1
【问题讨论】:
-
您能检查一下您的打包程序输出吗?如果这里有什么粘贴..
-
你好,打包器的输出是react-native run-android的输出吗?还是nodejs窗口的输出?
-
我从来没有见过没有
render()方法的 index.js 反应原生。这是你所有的代码吗?
标签: javascript android react-native