Skip to content

Commit 2a1dc07

Browse files
jimfbzpao
authored andcommitted
Merge pull request facebook#5870 from SimenB/patch-1
Remove the recommendation to use `isMounted` from beginner docs (cherry picked from commit e09dfe1)
1 parent 8c27faa commit 2a1dc07

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

docs/tips/12-initial-ajax.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ next: false-in-jsx.html
99

1010
Fetch data in `componentDidMount`. When the response arrives, store the data in state, triggering a render to update your UI.
1111

12-
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()`.
12+
When fetching data asynchronously, use `componentWillUnmount` to cancel any outstanding requests before the component is unmounted.
1313

1414
This example fetches the desired Github user's latest gist:
1515

@@ -23,15 +23,19 @@ var UserGist = React.createClass({
2323
},
2424

2525
componentDidMount: function() {
26-
$.get(this.props.source, function(result) {
27-
var lastGist = result[0];
28-
if (this.isMounted()) {
26+
this.setState({
27+
serverRequest: $.get(this.props.source, function(result) {
28+
var lastGist = result[0];
2929
this.setState({
3030
username: lastGist.owner.login,
3131
lastGistUrl: lastGist.html_url
3232
});
33-
}
34-
}.bind(this));
33+
}.bind(this))
34+
});
35+
},
36+
37+
componentWillUnmount: function() {
38+
this.state.serverRequest.abort();
3539
},
3640

3741
render: function() {

0 commit comments

Comments
 (0)