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

前端之家收集整理的这篇文章主要介绍了reactjs – React.js未捕获错误:不变违规前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
由于缺少对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.

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

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

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

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

export default (props) => {
    return <div>Hello World</div>;
}
@H_502_11@ 原文链接:https://www.f2er.com/react/300722.html

猜你在找的React相关文章