2016-08-04 3 views
0

Я пытаюсь расширить компонент и переопределить один метод, но оба этих метода не работают.Как создать активный компонент базы, который я могу расширить?

var Hello = React.createClass({ 
     getName: function() { throw "This method not implemented" }, 
     render: function() { 
      return <this.props.component.slug className='text'> 
       {this.props.component.value}{this.getName()} 
      </this.props.component.slug>; 
     } 
    }); 

    class HelloChild extends Hello { 
     constructor(props) { 
      super(props); 
     } 

     getName() 
     { 
      return "Child"; 
     } 
    }; 

Вторая попытка:

class Hello extends React.Component { 
     getName() { throw "This method not implemented" } 
     render() { 
      return <this.props.component.slug className='text'> 
       {this.props.component.value}{this.getName()} 
      </this.props.component.slug>; 
     } 
    }; 

    class HelloChild extends Hello { 
     constructor(props) { 
      super(props); 
     } 

     getName() 
     { 
      return "Child"; 
     } 
    }; 

Когда я пытаюсь оказать HelloChild, он выдает ошибку:

Uncaught Invariant Violation: Objects are not valid as a React child 
(found: object with keys {}). If you meant to render a collection of 
children, use an array instead or wrap the object using 
createFragment(object) from the React add-ons. Check the render method 
of `bound `. 

Как продлить его и только переопределить один метод?

ответ

0

Я не думаю, что вы терпите неудачу, пожалуйста, проверьте журнал ошибок снова, я думаю, что проблема this.props.component.value - это объект, а не строка или массив.

+0

нет. он работает, если я просто выполняю компонент Hello –

+0

, как насчет добавления 'this.getName = this.getName.bind (this)' в конструктор функции Hello Component. – chenkehxx

 Смежные вопросы

  • Нет связанных вопросов^_^