Browse Source

[tutorial] Use ids in comments data

main
Paul O’Shannessy 9 years ago
parent
commit
233fb7cf76
  1. 14
      docs/tutorial.md

14
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 author={comment.author}>
<Comment author={comment.author} key={comment.id}>
{comment.text}
</Comment>
);
@ -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({

Loading…
Cancel
Save