Skip to content

Commit

Permalink
Updated the ajax to tip to also mention that you need to check
Browse files Browse the repository at this point in the history
that the component is still mounted before updating it's state.

Closes facebook#1600.
  • Loading branch information
thaggie authored and sophiebits committed May 27, 2014
1 parent 03df8d7 commit cd3ba59
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions docs/tips/12-initial-ajax.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ 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.

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()`.

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

```js
Expand All @@ -25,10 +27,12 @@ var UserGist = React.createClass({
componentDidMount: function() {
$.get(this.props.source, function(result) {
var lastGist = result[0];
this.setState({
username: lastGist.owner.login,
lastGistUrl: lastGist.html_url
});
if (this.isMounted()) {
this.setState({
username: lastGist.owner.login,
lastGistUrl: lastGist.html_url
});
}
}.bind(this));
},

Expand Down

0 comments on commit cd3ba59

Please sign in to comment.