Created
January 16, 2017 03:53
-
-
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)
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 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