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

前端之家收集整理的这篇文章主要介绍了reactjs – React.js未捕获错误:不变违规前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
由于缺少对Commonjs模块的支持,从react_rails gem获得令人沮丧的感觉,我正在从netguru测试 react_webpack_rails gem.但从那以后,我得到了一个不变的违规.

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

  1. import React from 'react';
  2.  
  3. class tasksBox extends React.Component {
  4. render() {
  5. return <div>Hello World</div>;
  6. }
  7. }
  8.  
  9. export default tasksBox;

提出这些错误

  1. 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).
  2.  
  3. 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.

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

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

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

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

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

猜你在找的React相关文章