Remove the recommendation to use `isMounted` from beginner docs
Simen Bekkhus committed
Jan 17, 2016 at 10:54 UTC
1186cb9ab4e8e13a03d698c3a28f49b84fdc565e
1 file changed
+10
-6
docs/tips/12-initial-ajax.md
+10
-6
@@ -9,7 +9,7 @@ next: false-in-jsx.html
9
10
Fetch data in `componentDidMount`. When the response arrives, store the data in state, triggering a render to update your UI.
11
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.
13
14
This example fetches the desired Github user's latest gist:
15
@@ -23,15 +23,19 @@ var UserGist = React.createClass({
23
},
24
25
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];
29
this.setState({
30
username: lastGist.owner.login,
31
lastGistUrl: lastGist.html_url
32
});
33
- }
34
- }.bind(this));
33
+ }.bind(this))
34
+ });
35
+ },
36
+
37
+ componentWillUnmount: function() {
38
+ this.state.serverRequest.abort();
39
},
40
41
render: function() {