Skip to content

Instantly share code, notes, and snippets.

@melloc01
Last active August 29, 2015 14:17
Show Gist options
  • Save melloc01/c86011e38fe919b07975 to your computer and use it in GitHub Desktop.
Save melloc01/c86011e38fe919b07975 to your computer and use it in GitHub Desktop.
CommentBox
var xhr = new XMLHttpRequest(); //to reuse o intervals. Creating one for each request causes heavy CPU load
var CommentBox = React.createClass({
getInitialState: function() {
return {data: []};
},
loadCommentsFromServer: function() {
var cp = this;
xhr.open('GET', cp.props.url, true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState==4 && xhr.status==200){
cp.setState({data: JSON.parse(xhr.responseText)});
}
}
},
componentDidMount: function() {
this.loadCommentsFromServer();
this.interval = setInterval(this.loadCommentsFromServer, this.props.pollInterval);
},
componentWillUnmount: function() {
clearInterval(this.interval);
},
render: function() {
return (
<div className="commentBox">
<h1>Comments</h1>
<CommentList data={this.state.data} />
<CommentForm />
</div>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment