From 3bcb0d63f0637b6d0f619d1d04e0536e1f2e02aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Tue, 10 Nov 2015 16:12:49 -0800 Subject: [PATCH] [tutorial] Use ids in comments data --- docs/docs/tutorial.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/docs/tutorial.md b/docs/docs/tutorial.md index cded19081f..55b6e0b86f 100644 --- a/docs/docs/tutorial.md +++ b/docs/docs/tutorial.md @@ -292,8 +292,8 @@ So far we've been inserting the comments directly in the source code. Instead, l ```javascript // tutorial8.js var data = [ - {author: "Pete Hunt", text: "This is one comment"}, - {author: "Jordan Walke", text: "This is *another* comment"} + {id: 1, author: "Pete Hunt", text: "This is one comment"}, + {id: 2, author: "Jordan Walke", text: "This is *another* comment"} ]; ``` @@ -325,9 +325,9 @@ Now that the data is available in the `CommentList`, let's render the comments d // tutorial10.js var CommentList = React.createClass({ render: function() { - var commentNodes = this.props.data.map(function (comment) { + var commentNodes = this.props.data.map(function(comment) { return ( - + {comment.text} ); @@ -659,7 +659,7 @@ var CommentBox = React.createClass({ Our application is now feature complete but it feels slow to have to wait for the request to complete before your comment appears in the list. We can optimistically add this comment to the list to make the app feel faster. -```javascript{17-19} +```javascript{17-23} // tutorial20.js var CommentBox = React.createClass({ loadCommentsFromServer: function() { @@ -677,6 +677,10 @@ var CommentBox = React.createClass({ }, 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({