简体   繁体   English

React:警告:React.createElement:类型无效

[英]React: Warning: React.createElement: type is invalid

So I just started with react and I was going through one of the videos about "Handling Dynamic content in react). 因此,我刚开始使用react,并且正在观看有关“处理react中的动态内容”的视频之一。

So our main goal is to display three boxes using map function when we click on a button. 因此,我们的主要目标是在单击按钮时使用地图功能显示三个框。

We Initially created State Object like this 我们最初像这样创建状态对象

state = {
    person: [
    {name: "Rohit", age: 24},
    {name: "Hariom", age: 23},
    {name: "Vaibhav", age: 58}
  ],
  someOtherState: "Untouched state",
  showPerson: false
}

And then we tried something like this for conditional and mapping 然后我们针对条件和映射尝试了类似的方法

 render() {

    let person = null;

    if (this.state.showPerson) {
      person = (
        <div>
          {this.state.person.map(Person => {
            return <Person
            name={Person.name}
            age={Person.age} />
          })}
          </div>
     );
    }

Followed by this 其次是

return (

  <div className="App">
        <h1> Hi I am react App</h1>
        <button onClick={this.togglerPersonHandler}>Button</button>
        {person}
    </div>
      );
   }
}

Now, When I click on a button it throws following error (three times) 现在,当我单击一个按钮时,它会引发以下错误(三次)

index.js:2178 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

Check your code at App.js:54.
    in App (at index.js:7)

and

index.js:2178 The above error occurred in the <div> component:
    in div (at App.js:52)
    in div (at App.js:63)
    in App (at index.js:7)

Consider adding an error boundary to your tree to customize error handling behavior.

What am I doing wrong here? 我在这里做错了什么? (THe togglerPersonHandler simply changes the value of someOtherState). (togglerPersonH​​andler只是更改someOtherState的值)。 Do let me know my mistake and fix. 让我知道我的错误并解决。

Issue is in this part: 问题在这部分:

{this.state.person.map(Person => { return <Person .....})

Don't forgot that, we write jsx that is syntactic sugar, it will get converted into: 别忘了,我们编写的jsx是语法糖,它将转换为:

React.createElement(type, props, children)

Type should be either a string for html tags, or react component. 类型应该是html标签的字符串,或者是react组件。

So in your case, you are using Person as an argument to arrow function means its value will be each object of the this.state.persons array. 因此,在您的情况下,您使用Person作为箭头函数的参数,这意味着其值将是this.state.persons数组的每个对象。 At the same time you are rendering <Person /> , and Person is an object, not a valid type, that's why you are getting error: 同时,您正在渲染<Person /> ,并且Person是一个对象,而不是有效的类型,这就是为什么会出现错误的原因:

React.createElement: type is invalid React.createElement:类型无效

Solution will be, if (Person is a react component, you defined somewhere), use a different name: 解决方案是,如果(您是在某个位置定义的人是一个React组件),请使用其他名称:

<div>
    {
        this.state.person.map(el => (
            <Person
                key={el.name}
                name={el.name}
                age={el.age} />
            )
        )
    }
</div>

Also assign a unique key to each element. 还要为每个元素分配一个唯一的键。

I Agree with @Mayank Shukla. 我同意@Mayank Shukla。 Although a simpler, less complicated way of doing this would be 尽管更简单,更简单的方法是

    render() {
    let person = this.state.person.map(per => <Person key={per.name} name={per.name} age={per.age}/>);

    return (  <div className="App">
        <h1> Hi I am react App</h1>
        <button onClick={this.togglerPersonHandler}>Button</button>
        {this.state.showPerson ? person : ''}
    </div>);
}

} }

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

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