【问题标题】:Uncaught Error: Invariant Violation: Element type is invalid: expected a string or a class/function but got: object未捕获的错误:不变违规:元素类型无效:预期为字符串或类/函数,但得到:对象
【发布时间】:2017-10-13 13:02:43
【问题描述】:

我是 React-Native 的新手,并尝试按照初学者的教程开始使用它。我按照教程创建了一个“Header”组件并尝试导出和导入它并最终使用它。不幸的是,我收到一个错误:未捕获的错误:不变违规:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数,但得到:对象。 我遇到了这两个问题:

  1. Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object
  2. 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


【解决方案1】:

所以我想通了,问题是我注册了两次App:

AppRegistry.registerComponent('albums', () => App);

一次在 index.js 中,第二次在 App.android.js 中。 我通过从 index.js 中删除注册解决了错误。

无论如何感谢帮助者:)

【讨论】:

  • 很容易错过。其他人都这样做了。
【解决方案2】:

改变这个表格:

import { Header } from './src/components/header';

到:

import  Header  from './src/components/header';

在 index.js 中更改:

import App from './App';

import App from './App.android'; 

希望这会有所帮助。

【讨论】:

  • 正如我所写,我已经尝试了这两个选项,但都没有解决错误。
  • 试过你写的但还没有解决错误
猜你喜欢
  • 1970-01-01
  • 2016-03-27
  • 2016-07-29
  • 2019-12-31
  • 2020-09-07
  • 1970-01-01
  • 2020-07-18
  • 2019-05-09
  • 2020-06-27
相关资源
最近更新 更多