簡體   English   中英

“不要設置 React 組件的道具”警告

[英]"Don't set props of the React component" warning

我收到這個 React.js 錯誤,我很想知道如何修復它。 有沒有辦法在this上調用React.cloneElement

警告:不要設置 React 組件的 .props.children 。 相反,在最初創建元素時指定正確的值,或者使用 React.cloneElement 使用更新的 props 創建一個新元素。 該元素是由七創造的。

這是一個完整的工作示例。

這是我的代碼:

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>
    )
  }
})

正如@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>
    )
  }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM