Browse Source

Remove the recommendation to use `isMounted` from beginner docs

main
Simen Bekkhus 9 years ago
parent
commit
2f2f289815
  1. 16
      tips/12-initial-ajax.md

16
tips/12-initial-ajax.md

@ -9,7 +9,7 @@ next: false-in-jsx.html
Fetch data in `componentDidMount`. When the response arrives, store the data in state, triggering a render to update your UI. Fetch data in `componentDidMount`. When the response arrives, store the data in state, triggering a render to update your UI.
When processing the response of an asynchronous request, be sure to check that the component is still mounted before updating its state by using `this.isMounted()`. When fetching data asynchronously, use `componentWillUnmount` to cancel any outstanding requests before the component is unmounted.
This example fetches the desired Github user's latest gist: This example fetches the desired Github user's latest gist:
@ -23,15 +23,19 @@ var UserGist = React.createClass({
}, },
componentDidMount: function() { componentDidMount: function() {
$.get(this.props.source, function(result) { this.setState({
var lastGist = result[0]; serverRequest: $.get(this.props.source, function(result) {
if (this.isMounted()) { var lastGist = result[0];
this.setState({ this.setState({
username: lastGist.owner.login, username: lastGist.owner.login,
lastGistUrl: lastGist.html_url lastGistUrl: lastGist.html_url
}); });
} }.bind(this))
}.bind(this)); });
},
componentWillUnmount: function() {
this.state.serverRequest.abort();
}, },
render: function() { render: function() {

Loading…
Cancel
Save