@samitouri / QOS-React-2 / commits / cd3ba59cf1

Updated the ajax to tip to also mention that you need to check that the component is still mounted before updating it's state.

Updated the ajax to tip to also mention that you need to check that the component is still mounted before updating it's state. Closes #1600.

Tom Haggie committed May 25, 2014 at 10:03 UTC cd3ba59cf18c08338f6eb614ee28daa30ac974de
1 file changed +8 -4
docs/tips/12-initial-ajax.md
+8 -4
@@ -9,6 +9,8 @@ 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()`.
13 +
14 This example fetches the desired Github user's latest gist:
15
16 ```js
@@ -25,10 +27,12 @@ var UserGist = React.createClass({
27 componentDidMount: function() {
28 $.get(this.props.source, function(result) {
29 var lastGist = result[0];
28 - this.setState({
29 - username: lastGist.owner.login,
30 - lastGistUrl: lastGist.html_url
31 - });
30 + if (this.isMounted()) {
31 + this.setState({
32 + username: lastGist.owner.login,
33 + lastGistUrl: lastGist.html_url
34 + });
35 + }
36 }.bind(this));
37 },
38