【问题标题】:'A valid React element (or null) must be returned' error with one of the component in ReactJS“必须返回有效的 React 元素(或 null)”ReactJS 中的组件之一错误
【发布时间】:2016-05-06 13:07:29
【问题描述】:

我的代码是这样的

var data = [
    {id: 1, taskName: "Pete Hunt", standarDescription: "This is one comment", emplComment: "meaaow I am meeawo", empRating : "1"},
    {id: 2, taskName: "Pete Hunt", standarDescription: "This is one comment", emplComment: "meaaow I am meeawo", empRating : "1"},
    {id: 3, taskName: "Pete Hunt", standarDescription: "This is one comment", emplComment: "meaaow I am meeawo", empRating : "1"},
    {id: 4, taskName: "Pete Hunt", standarDescription: "This is one comment", emplComment: "meaaow I am meeawo", empRating : "1"},
    {id: 5, taskName: "Pete Hunt", standarDescription: "This is one comment", emplComment: "meaaow I am meeawo", empRating : "1"}
];

var Tableforbasictask = React.createClass({
  render: function() {
    return (
     <div className="downloadlinks">
      <table className="table table-bordered table-striped-col nomargin" id="table-data">
      <tbody>
        <tr align="center">
            <td>Task  Name</td>
            <td>Standard Discription of Task</td>
            <td>Employee Comment</td>
            <td>Employee rating</td>
        </tr>
        <TableforbasictaskList data={this.props.data} />
        <TableforbasictaskForm />
        </tbody>
      </table>
      </div>
    );
  }
});

var TableforbasictaskForm = React.createClass({
    render: function() {
      return (
        <div className="commentForm">
          Hello, world! I am a CommentForm.
        </div>
      );
    }
  });

var Addcontenttotable = React.createClass({
render: function() {
  return (
    <tr><td>{this.props.taskName}</td>
        <td>{this.props.standarDescription}</td>
        <td>{this.props.emplComment}</td>
        <td>{this.props.empRating}</td>
    </tr>
  );
}
});

var TableforbasictaskList = React.createClass({
render: function() {
  var commentNodes = this.props.data.map(function(comment) {
    return (
      <Addcontenttotable taskName={comment.taskName} standarDescription={comment.standarDescription} emplComment={comment.emplComment} empRating={comment.empRating} key={comment.id}>
      </Addcontenttotable>
    );
  });
  return (
      {commentNodes}
  );
}
});
ReactDOM.render(<div><Tableforbasictask data={data}  /></div>, document.getElementById('content'));

我要做的就是将Json 数据中的详细信息列在表格中。我将在未来添加一个 API 来获取该 JSON

但我收到以下错误

错误:TableforbasictaskList.render():必须返回有效的 React 元素(或 null)。您可能返回了未定义、数组或其他一些无效对象。

这里是JSFIDDLE

感谢任何帮助

【问题讨论】:

  • 您是否尝试过在这个问题中甚至在小提琴编辑器中格式化您的代码?
  • @The Pardon 我现在修复了 Fiddle Link 请看一下

标签: javascript reactjs


【解决方案1】:

React 组件必须have only one root node.,因为你在table 中使用TableforbasictaskList,你需要将commentNodes 包裹在&lt;tbody&gt;. 中,同样在Tableforbasictask 中移动TableforbasictaskFormtable

var TableforbasictaskList = React.createClass({
  render: function() {
    // .....
    return (<tbody>{commentNodes}</tbody>);
  }
});

var Tableforbasictask = React.createClass({
  render: function() {
    return <div className="downloadlinks">
      <table
        className="table table-bordered table-striped-col nomargin"
        id="table-data"
      >
        <thead>
          <tr align="center">
            <td>Task  Name</td>
            <td>Standard Discription of Task</td>
            <td>Employee Comment</td>
            <td>Employee rating</td>
          </tr>
        </thead>
        <TableforbasictaskList data={this.props.data} /> 
      </table>
      <TableforbasictaskForm />
    </div>
  }
});

Example

【讨论】:

  • 谢谢我明白了,谢谢我是一个 PHP 开发者,所以在 ReactJs 中工作有点 PAA ;),但我相信我会掌握它
【解决方案2】:

render 应该返回单个根元素 https://jsfiddle.net/69z2wepo/41120/

return (<div>
    {commentNodes}
</div>);

更新。使用 tbody 作为包装器的更有效选项

return (<tbody>
    {commentNodes}
</tbody>);

https://jsfiddle.net/69z2wepo/41125/

【讨论】:

  • @YuryTarbanko 但它没有将值正确映射到正确的列,你能指导我怎么做吗?
  • 不确定你的意思。您已经在表格中有 div,这是不行的。 :) 您可以使用trtd,具体取决于您需要如何排列元素。或者更好的是,您可以使用 div 代替并使用样式来制作布局。
猜你喜欢
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
相关资源
最近更新 更多