Skip to content

Commit a3b3409

Browse files
committed
setInterval -> window.setInterval and remove ugly window check by using componentDidMount.
1 parent 8aebb47 commit a3b3409

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

site/jekyll/getting-started/tutorial.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ var CommentBox = React.createClass({
529529
},
530530
componentWillMount: function() {
531531
this.loadCommentsFromServer();
532-
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
532+
window.setInterval(this.loadCommentsFromServer, this.props.pollInterval);
533533
},
534534
render: function() {
535535
return (
@@ -657,7 +657,7 @@ var CommentBox = React.createClass({
657657
},
658658
componentWillMount: function() {
659659
this.loadCommentsFromServer();
660-
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
660+
window.setInterval(this.loadCommentsFromServer, this.props.pollInterval);
661661
},
662662
render: function() {
663663
return (
@@ -734,7 +734,7 @@ var CommentBox = React.createClass({
734734
},
735735
componentWillMount: function() {
736736
this.loadCommentsFromServer();
737-
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
737+
window.setInterval(this.loadCommentsFromServer, this.props.pollInterval);
738738
},
739739
render: function() {
740740
return (
@@ -794,7 +794,7 @@ var CommentBox = React.createClass({
794794
},
795795
componentWillMount: function() {
796796
this.loadCommentsFromServer();
797-
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
797+
window.setInterval(this.loadCommentsFromServer, this.props.pollInterval);
798798
},
799799
render: function() {
800800
return (
@@ -875,9 +875,9 @@ If you go to this URL in your browser, you should notice that the code has been
875875

876876
Server-side rendering means that your application initially renders the components on the server-side, rather than fetching data from the server and rendering using JavaScript. This enhances the performance of your application since the user will see the initial state immediately.
877877

878-
We need to make some motifications to `CommentBox` to support server-side rendering. Firstly, we need to accept an `initialData` prop, which will be used to set the initial state of the component, rather than doing an AJAX request. We also need to ensure the `setInterval` call for polling for new comments is only executed client-side. Finally, we will remove the `loadCommentsFromServer` call from `getInitialState`, since it is no longer required.
878+
We need to make some motifications to `CommentBox` to support server-side rendering. Firstly, we need to accept an `initialData` prop, which will be used to set the initial state of the component, rather than doing an AJAX request. We also need to ensure the `setInterval` call for polling for new comments is only executed client-side, by moving it to the `componentDidMount` method. Finally, we will remove the `loadCommentsFromServer` call from `getInitialState`, since it is no longer required.
879879

880-
```javascript{28,31-35}
880+
```javascript{28,30-32}
881881
var CommentBox = React.createClass({
882882
loadCommentsFromServer: function() {
883883
var xhr = new XMLHttpRequest();
@@ -907,12 +907,8 @@ var CommentBox = React.createClass({
907907
getInitialState: function() {
908908
return { data: this.props.initialData };
909909
},
910-
componentWillMount: function() {
911-
//this.loadCommentsFromServer();
912-
// Only do this in the browser, not on the server-side.
913-
if (typeof window !== 'undefined') {
914-
window.setInterval(this.loadCommentsFromServer, this.props.pollInterval);
915-
}
910+
componentDidMount: function() {
911+
window.setInterval(this.loadCommentsFromServer, this.props.pollInterval);
916912
},
917913
render: function() {
918914
return (

0 commit comments

Comments
 (0)