Skip to content

Instantly share code, notes, and snippets.

@edwardwbli
Created January 16, 2017 03:53
Show Gist options
  • Save edwardwbli/2a5883495afbf39f0d5a528a8cb52274 to your computer and use it in GitHub Desktop.
Save edwardwbli/2a5883495afbf39f0d5a528a8cb52274 to your computer and use it in GitHub Desktop.
Sampel for react component to use ajax to call api and then update state (JSX, need babel for translation)
var CommentBox = React.createClass({
getInitalState: function() {
return {data: []};
},
componentDidMount: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
success: funciton(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>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment