"不要设置React组件的道具"警告

时间:2015-09-21 10:34:56

标签: javascript reactjs

我收到此React.js错误我很想知道如何修复它。有没有办法在React.cloneElement上致电this

  

警告:不要设置React组件的.props.children。相反,在最初创建元素时指定正确的值,或使用React.cloneElement创建具有更新的props的新元素。该元素由Seven创建。

Here's a full working example.

这是我的代码:

var Thead = React.createClass({
  displayName: 'Thead',
  propTypes: function () {
    return {
      children: React.propTypes.node
    }
  },
  componentWillMount: function () {
    this.childTr = containsElement('tr', this.props.children)
    this.props.children = reconcileChildren('th',
      (this.childTr) ? this.childTr.props.children : this.props.children,
      this.props.data
    )
  },
  render: function () {
    return (
      <thead>
        {this.childTr ? <tr {...this.childTr.props}>
          {this.props.children}
        </tr> : <tr>
          {this.props.children}
        </tr>}
      </thead>
    )
  }
})

1 个答案:

答案 0 :(得分:0)

正如@fuyushimoya在评论中所说,不鼓励改变props(我不知道)我将孩子设置为this.children

var Thead = React.createClass({
  displayName: 'Thead',
  propTypes: function () {
    return {
      children: React.propTypes.node
    }
  },
  componentWillMount: function () {
    this.childTr = containsElement('tr', this.props.children)
    this.children = reconcileChildren('th',
      (this.childTr) ? this.childTr.props.children : this.props.children,
      this.props.data
    )
  },
  render: function () {
    return (
      <thead>
        {this.childTr ? <tr {...this.childTr.props}>
          {this.children}
        </tr> : <tr>
          {this.children}
        </tr>}
      </thead>
    )
  }
})