2016-04-20 77 views
0

我想知道爲什麼在Facebook的反應JS教程(https://facebook.github.io/react/docs/tutorial.html#optimization-optimistic-updates)我們不只是添加this.loadCommentsFromServer();之後在handleCommentSubmit的ajax查詢?教程reactjs:優化

感謝

我想要做什麼:

var CommentBox = React.createClass({ 
    loadCommentsFromServer: function() { 
    $.ajax({ 
     url: this.props.url, 
     dataType: 'json', 
     cache: false, 
     success: function(data) { 
     this.setState({data: data}); 
     }.bind(this), 
     error: function(xhr, status, err) { 
     console.error(this.props.url, status, err.toString()); 
     }.bind(this) 
    }); 
    }, 
    handleCommentSubmit: function(comment) { 
    $.ajax({ 
     url: this.props.url, 
     dataType: 'json', 
     type: 'POST', 
     data: comment, 
     success: function(data) { 
     this.setState({data: data}); 
     }.bind(this), 
     error: function(xhr, status, err) { 
     console.error(this.props.url, status, err.toString()); 
     }.bind(this) 
    }); 
    this.loadCommentsFromServer(); 
    }, 
    getInitialState: function() { 
    return {data: []}; 
    }, 
    componentDidMount: function() { 
    this.loadCommentsFromServer(); 
    setInterval(this.loadCommentsFromServer, this.props.pollInterval); 
    }, 
    render: function() { 
    return (
     <div className="commentBox"> 
     <h1>Comments</h1> 
     <CommentList data={this.state.data} /> 
     <CommentForm onCommentSubmit={this.handleCommentSubmit} /> 
     </div> 
    ); 
    } 
}); 

做什麼FB:

var CommentBox = React.createClass({ 
    loadCommentsFromServer: function() { 
    $.ajax({ 
     url: this.props.url, 
     dataType: 'json', 
     cache: false, 
     success: function(data) { 
     this.setState({data: data}); 
     }.bind(this), 
     error: function(xhr, status, err) { 
     console.error(this.props.url, status, err.toString()); 
     }.bind(this) 
    }); 
    }, 
    handleCommentSubmit: function(comment) { 
    var comments = this.state.data; 
    // Optimistically set an id on the new comment. It will be replaced by an 
    // id generated by the server. In a production application you would likely 
    // not use Date.now() for this and would have a more robust system in place. 
    comment.id = Date.now(); 
    var newComments = comments.concat([comment]); 
    this.setState({data: newComments}); 
    $.ajax({ 
     url: this.props.url, 
     dataType: 'json', 
     type: 'POST', 
     data: comment, 
     success: function(data) { 
     this.setState({data: data}); 
     }.bind(this), 
     error: function(xhr, status, err) { 
     this.setState({data: comments}); 
     console.error(this.props.url, status, err.toString()); 
     }.bind(this) 
    }); 
    }, 
    getInitialState: function() { 
    return {data: []}; 
    }, 
    componentDidMount: function() { 
    this.loadCommentsFromServer(); 
    setInterval(this.loadCommentsFromServer, this.props.pollInterval); 
    }, 
    render: function() { 
    return (
     <div className="commentBox"> 
     <h1>Comments</h1> 
     <CommentList data={this.state.data} /> 
     <CommentForm onCommentSubmit={this.handleCommentSubmit} /> 
     </div> 
    ); 
    } 
}); 
+0

也許你可以在這裏發佈的相關代碼的較大塊。 – GMchris

回答

0

根據他們提供的服務器的源代碼,POSTreturns the new comments after updating

你可以看你所指定的教程承擔這個,因爲反應似乎放回狀態如下: success: function(data) { this.setState({data: data}); }

+0

因此,如果使用POST的響應更新狀態,爲什麼我們需要本教程中描述的優化? 當我刪除我的優化,評論立即更新......這不是我的優化之前。 – Nrvnqsr

+0

即使在「POST」之前,新評論也會添加到列表中,所以它會立即顯示在列表中。沒有它,你將不得不等待'POST'完成並返回數據以顯示在列表中。這就是爲什麼它被標記爲樂觀更新。 – Kujira