react网络数据获取

来源:互联网 发布:哪个软件可以看素媛 编辑:程序博客网 时间:2024/06/05 09:26


// tutorial14.jsvar CommentBox = React.createClass({  loadCommentsFromServer: function() {    $.ajax({      url: this.props.url,      dataType: 'json',      cache: false,      success: function(data) {        this.setState({data: data});      }.bind(this),      error: function(xhr, status, err) {        console.error(this.props.url, status, err.toString());      }.bind(this)    });  },  getInitialState: function() {    return {data: []};  },  componentDidMount: function() {    this.loadCommentsFromServer();    setInterval(this.loadCommentsFromServer, this.props.pollInterval);  },  render: function() {    return (      <div className="commentBox">        <h1>Comments</h1>        <CommentList data={this.state.data} />        <CommentForm />      </div>    );  }});ReactDOM.render(  <CommentBox url="/api/comments" pollInterval={2000} />,  document.getElementById('content'));


0 0
原创粉丝点击