2015-09-21 4 views
-2

«Не устанавливайте опоры компонента React». Я получаю эту ошибку React.js. Мне бы хотелось знать, как ее исправить. Есть ли способ позвонить React.cloneElement по телефону this?Предупреждение

Предупреждение: не устанавливайте .props.children компонента React. Вместо этого укажите правильное значение при первоначальном создании элемента или используйте React.cloneElement, чтобы создать новый элемент с обновленными реквизитами. Элемент был создан Семь.

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

'this.props.children = reconcileChildren', вы изменяете' prop' на этой строке, что сильно обескураживает. Реквизит должен передаваться только родителям, и вам, возможно, придется использовать что-то еще для хранения этих детей. – fuyushimoya

+0

Является ли 'reconcileChildren' простым выбором между' this.props.children' и 'this.childTr.props.children'? –

+0

@DavinTryon Нет, это не полный выход, вы можете проверить его [здесь] (https://jsbin.com/jofusa/3/edit?js,console,output). – ThomasReggi

ответ

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> 
    ) 
    } 
}) 
Смежные вопросы