加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React.js未捕获错误:不变违规

发布时间:2020-12-15 20:42:14 所属栏目:百科 来源:网络整理
导读:由于缺少对Commonjs模块的支持,从react_rails gem获得令人沮丧的感觉,我正在从netguru测试 react_webpack_rails gem.但从那以后,我得到了一个不变的违规. 例如,如果我在ES6语法中编写一个简单的Hello World组件: import React from 'react';class tasksBox
由于缺少对Commonjs模块的支持,从react_rails gem获得令人沮丧的感觉,我正在从netguru测试 react_webpack_rails gem.但从那以后,我得到了一个不变的违规.

例如,如果我在ES6语法中编写一个简单的Hello World组件:

import React from 'react';

class tasksBox extends React.Component {
  render() {
    return <div>Hello World</div>;
  }
}

export default tasksBox;

提出这些错误:

Warning: React.createElement: type should not be null,undefined,boolean,or number. It should be a string (for DOM elements) or a ReactClass (for composite components).

Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

您的帮助将非常感谢,我无法确定错误来自哪里.

您的反应组件的名称无效.它必须像这样大写:
export class TasksBox extends React.Component {
    render() {
        return <div>Hello World</div>;
    }
}

您可以直接导出类.注意我改名为大写字母TasksBox而不是tasksBox,因为React希望您的类名以大写字母开头

编辑:如果你的例子没有状态或其他自定义的函数,你不需要这是一个React类..但它可以是一个无状态函数/组件像这样

export default (props) => {
    return <div>Hello World</div>;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读