From 774f5a022eacbb054b09aa5a7d596186874b2600 Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Wed, 9 Jul 2014 12:02:54 -0700 Subject: [PATCH] tutorial: Fetch data in componentDidMount instead In a world where this component was server-rendered, we wouldn't want to call the data-fetching code there so it makes more sense to have it in componentDidMount. --- docs/docs/tutorial.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/docs/tutorial.md b/docs/docs/tutorial.md index 979545e8f2..9be7e42724 100644 --- a/docs/docs/tutorial.md +++ b/docs/docs/tutorial.md @@ -386,7 +386,7 @@ var CommentBox = React.createClass({ getInitialState: function() { return {data: []}; }, - componentWillMount: function() { + componentDidMount: function() { $.ajax({ url: this.props.url, dataType: 'json', @@ -410,7 +410,7 @@ var CommentBox = React.createClass({ }); ``` -Here, `componentWillMount` is a method called automatically by React before a component is rendered. The key to dynamic updates is the call to `this.setState()`. We replace the old array of comments with the new one from the server and the UI automatically updates itself. Because of this reactivity, it is only a minor change to add live updates. We will use simple polling here but you could easily use WebSockets or other technologies. +Here, `componentDidMount` is a method called automatically by React when a component is rendered. The key to dynamic updates is the call to `this.setState()`. We replace the old array of comments with the new one from the server and the UI automatically updates itself. Because of this reactivity, it is only a minor change to add live updates. We will use simple polling here but you could easily use WebSockets or other technologies. ```javascript{3,19-20,34} // tutorial14.js @@ -430,7 +430,7 @@ var CommentBox = React.createClass({ getInitialState: function() { return {data: []}; }, - componentWillMount: function() { + componentDidMount: function() { this.loadCommentsFromServer(); setInterval(this.loadCommentsFromServer, this.props.pollInterval); }, @@ -538,7 +538,7 @@ var CommentBox = React.createClass({ getInitialState: function() { return {data: []}; }, - componentWillMount: function() { + componentDidMount: function() { this.loadCommentsFromServer(); setInterval(this.loadCommentsFromServer, this.props.pollInterval); }, @@ -613,7 +613,7 @@ var CommentBox = React.createClass({ getInitialState: function() { return {data: []}; }, - componentWillMount: function() { + componentDidMount: function() { this.loadCommentsFromServer(); setInterval(this.loadCommentsFromServer, this.props.pollInterval); }, @@ -668,7 +668,7 @@ var CommentBox = React.createClass({ getInitialState: function() { return {data: []}; }, - componentWillMount: function() { + componentDidMount: function() { this.loadCommentsFromServer(); setInterval(this.loadCommentsFromServer, this.props.pollInterval); },