Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Created January 23, 2016 08:39
Show Gist options
  • Save StevenJL/9d1c1d805c9c1ae591f3 to your computer and use it in GitHub Desktop.
Save StevenJL/9d1c1d805c9c1ae591f3 to your computer and use it in GitHub Desktop.
var CommentBox = React.createClass({
// getInitialState is called right when the component is initialized
// its return value is set as the component's state
getInitialState: function() {
return {data: []};
},
// componentDidMount is called right when the DOM element is first mounted
// it is not called again every time the component re-renders
componentDidMount: function() {
$.ajax({
// using the url property we set above
url: this.props.url,
dataType: 'json',
cache: false,
success: function(data) {
// as mentioned earlier, right after setState is called
// the component will re-render
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>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment