Last active
August 29, 2015 14:17
-
-
Save melloc01/c86011e38fe919b07975 to your computer and use it in GitHub Desktop.
CommentBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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