为什么我的控制台说“未能构建'评论'?

时间:2017-07-30 13:05:49

标签: reactjs

错误看起来像这样 -

无法构造'Comment':请使用'new'运算符,此DOM对象构造函数不能作为函数调用

不明白如何使用“新”运算符,这是什么意思!有人可以帮帮我吗?

这是代码 -

import React from "react";
import {render} from "react-dom";



class Board extends React.Component{
  state ={comments: ["I like bla", "what next?", "this is the last."]}

  eachComment(text, i){
    return (<Comment key={i}>{text}</Comment>);
  }

  render(){
    return(
      <div>
        {
          this.state.comments.map(this.eachComment)
        }
      </div>
    );
  }
}

render(<Board/>, window.document.getElementById("example"));

3 个答案:

答案 0 :(得分:0)

应该是

constructor() {
this.state ={comments: ["I like bla", "what next?", "this is the last."]}
}

而不是state ={comments: ["I like bla", "what next?", "this is the last."]}

答案 1 :(得分:0)

在一个状态类中它应该是this.state =这是为了让类保存数据并且react组件正在查找类中的状态

答案 2 :(得分:0)

您可以尝试这些更改吗?

class Board extends React.Component{
  constructor(){
     super();
     this.state ={comments: ["I like bla", "what next?", "this is the last."]};
  }
...
}

并将eachComment指定为render()

上方的对象

参考:https://stackoverflow.com/a/28205521/3279391