Подтвердить что ты не робот

React proptypes component vs element

В чем разница между:

var Icon = React.createClass({
  propTypes: {
    name: React.PropTypes.string
  },
  render: function(){
    return (
      <span className={'glyphicon glyphicon-'+this.props.name}></span>
    );
  }
});

var Button = React.createClass({
  propTypes: {
    content: React.PropTypes.element // This one?
    content: React.PropTypes.component // Or this one?
  },
  render: function() {
    return (
       <button>{content}</button>
    );
  }
});

Я бы хотел использовать;

<Button content={<Icon name="heart" />} />

В каком случае я должен использовать другое?

Спасибо

4b9b3361

Ответ 1

Я думаю, что вы должны сделать Button следующим образом:

var Button = React.createClass({
  render: function() {
    return (
       <button>{this.props.children}</button>
    );
  }
});

Затем вы можете использовать его с помощью <Icon /> или всего остального (текст...):

<Button><Icon name="heart" /></Button>

или

<Button>anything</Button>

И тогда правильный proptype зависит от того, что вы хотите разрешить внутри <Button /> (Но я не уверен, что React.PropTypes.component существует, это не в документации: http://facebook.github.io/react/docs/reusable-components.html)