简体   繁体   English

高阶组件:React.createElement:类型无效

[英]High Order Component: React.createElement: type is invalid

I have a component someComponent , and a function withMoreStuff takes in a class, returns a class. 我有一个组件someComponent和功能withMoreStuff发生在一类,则返回一个类。 I have checked all my import/exports, no issue with any default or named export. 我已经检查了所有的导入/导出,没有任何默认或命名导出的问题。

error: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. 错误: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

// @flow
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { withMoreStuff } from 'hocs';
import { someComponent } from 'components';

type PropsType = {
  data: [{ id: number }],
}

const wrappedComponent = withMoreStuff(someComponent);

class ItemList extends Component {
  props: PropsType;

  renderRows(data: any) {
    return data.map( item =>
        <wrappedComponent key={item.id} data={item} />
    );
  }

  render() {

    return (
      <div>
        {this.renderRows(this.props.data)}
      </div>
    );
  }
};

export default ItemList;

The error returned from this file is saying the wrappedComponent is not a valid class. 从此文件返回的错误是说wrappedComponent不是有效的类。

If I move the line const wrappedComponent = withMoreStuff(someComponent); 如果我将行const wrappedComponent = withMoreStuff(someComponent); into the render method it would render properly: 进入render方法,它将正确渲染:

render() {
    const wrappedComponent = withMoreStuff(someComponent);
    return (
      <div>
        {this.renderRows(this.props.data)}
      </div>
    );
}

WHY??? 为什么???

I believe you are not receiving any props and your renderRows method is not returning anything, check if this solves it : 我相信您没有收到任何道具,并且您的renderRows方法没有返回任何内容,请检查是否可以解决该问题:

renderRows(data: any) {
    if( !data ) return </div>; 
    return data.map( item =>
        <wrappedComponent key={item.id} data={item} />
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM