React Props与State

2016-08-12  本文已影响0人  wmtcore

Props

验证props
propTypes: {
    style: View.propTypes.style.isRequired,
    elementStyle: View.propTypes.style,
  }

//isRequired 可选

State

var CommentBox = React.createClass({
  getInitialState: function() {
    return {data: []};
  },
  componentDidMount: function() {
    $.ajax({
      url: this.props.url,
      dataType: 'json',
      success: function(data) {
        this.setState({data: data});
      }.bind(this),
      error: function(xhr, status, err) {
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    });
  },
  render: function() {
    return (
      <div className="commentBox">
        <h1>Comments</h1>
        <CommentList data={this.state.data} />
        <CommentForm />
      </div>
    );
  }
});

上一篇下一篇

猜你喜欢

热点阅读