Переглянути джерело

Listing 14: updating state repeatedly.

Frederic G. MARAND 8 роки тому
батько
коміт
6c4bc3c094
1 змінених файлів з 6 додано та 3 видалено
  1. 6 3
      public/scripts/tutorial.js

+ 6 - 3
public/scripts/tutorial.js

@@ -43,16 +43,19 @@ let CommentForm = React.createClass({
 });
 
 let CommentBox = React.createClass({
+  componentDidMount: function () {
+    this.loadCommentsFromServer();
+    setInterval(this.loadCommentsFromServer, this.props.pollInterval);
+  },
   getInitialState: function () {
     return { data: [] };
   },
-  componentDidMount: function () {
+  loadCommentsFromServer: function () {
     $.ajax({
       url: this.props.url,
       dataType: "json",
       cache: false,
       success: function (result) {
-        console.log('Received frop api: ', result);
         this.setState({ data: result });
       }.bind(this),
       error: function (xhr, status, err) {
@@ -72,6 +75,6 @@ let CommentBox = React.createClass({
 });
 
 ReactDOM.render(
-  <CommentBox url="/api/comments" />,
+  <CommentBox url="/api/comments" pollInterval={2000} />,
   document.getElementById("content")
 );