@@ -22,7 +22,7 @@ It'll also have a few neat features:
22
22
23
23
### Want to skip all this and just see the source?
24
24
25
- [ It's all on GitHub.] ( https://github.com/petehunt /react-tutorial )
25
+ [ It's all on GitHub.] ( https://github.com/reactjs /react-tutorial )
26
26
27
27
### Getting started
28
28
@@ -40,9 +40,7 @@ For this tutorial we'll use prebuilt JavaScript files on a CDN. Open up your fav
40
40
<body >
41
41
<div id =" content" ></div >
42
42
<script type =" text/jsx" >
43
- /* *
44
- * @jsx React.DOM
45
- */
43
+ /* * @jsx React.DOM */
46
44
// The above declaration must remain intact at the top of the script.
47
45
// Your code here
48
46
</script >
@@ -91,7 +89,7 @@ The first thing you'll notice is the XML-ish syntax in your JavaScript. We have
91
89
var CommentBox = React .createClass ({displayName: ' CommentBox' ,
92
90
render : function () {
93
91
return (
94
- React .DOM .div ({className: " commentBox" },
92
+ React .DOM .div ({className: " commentBox" },
95
93
" Hello, world! I am a CommentBox."
96
94
)
97
95
);
@@ -308,7 +306,11 @@ Now that the data is available in the `CommentList`, let's render the comments d
308
306
var CommentList = React.createClass({
309
307
render: function() {
310
308
var commentNodes = this.props.data.map(function (comment) {
311
- return <Comment author={comment.author}>{comment.text}</Comment>;
309
+ return (
310
+ <Comment author={comment.author}>
311
+ {comment.text}
312
+ </Comment>
313
+ );
312
314
});
313
315
return (
314
316
<div className="commentList">
@@ -491,11 +493,7 @@ var CommentForm = React.createClass({
491
493
return (
492
494
<form className="commentForm" onSubmit={this.handleSubmit}>
493
495
<input type="text" placeholder="Your name" ref="author" />
494
- <input
495
- type="text"
496
- placeholder="Say something..."
497
- ref="text"
498
- />
496
+ <input type="text" placeholder="Say something..." ref="text" />
499
497
<input type="submit" value="Post" />
500
498
</form>
501
499
);
@@ -549,9 +547,7 @@ var CommentBox = React.createClass({
549
547
<div className="commentBox">
550
548
<h1>Comments</h1>
551
549
<CommentList data={this.state.data} />
552
- <CommentForm
553
- onCommentSubmit={this.handleCommentSubmit}
554
- />
550
+ <CommentForm onCommentSubmit={this.handleCommentSubmit} />
555
551
</div>
556
552
);
557
553
}
@@ -575,11 +571,7 @@ var CommentForm = React.createClass({
575
571
return (
576
572
<form className="commentForm" onSubmit={this.handleSubmit}>
577
573
<input type="text" placeholder="Your name" ref="author" />
578
- <input
579
- type="text"
580
- placeholder="Say something..."
581
- ref="text"
582
- />
574
+ <input type="text" placeholder="Say something..." ref="text" />
583
575
<input type="submit" value="Post" />
584
576
</form>
585
577
);
@@ -630,9 +622,7 @@ var CommentBox = React.createClass({
630
622
<div className="commentBox">
631
623
<h1>Comments</h1>
632
624
<CommentList data={this.state.data} />
633
- <CommentForm
634
- onCommentSubmit={this.handleCommentSubmit}
635
- />
625
+ <CommentForm onCommentSubmit={this.handleCommentSubmit} />
636
626
</div>
637
627
);
638
628
}
@@ -661,18 +651,22 @@ var CommentBox = React.createClass({
661
651
handleCommentSubmit: function(comment) {
662
652
var comments = this.state.data;
663
653
var newComments = comments.concat([comment]);
664
- this.setState({data: newComments});
665
- $.ajax({
666
- url: this.props.url,
667
- dataType: 'json',
668
- type: 'POST',
669
- data: comment,
670
- success: function(data) {
671
- this.setState({data: data});
672
- }.bind(this),
673
- error: function(xhr, status, err) {
674
- console.error(this.props.url, status, err.toString());
675
- }.bind(this)
654
+ this.setState({data: newComments}, function() {
655
+ // `setState` accepts a callback. To avoid (improbable) race condition,
656
+ // `we'll send the ajax request right after we optimistically set the new
657
+ // `state.
658
+ $.ajax({
659
+ url: this.props.url,
660
+ dataType: 'json',
661
+ type: 'POST',
662
+ data: comment,
663
+ success: function(data) {
664
+ this.setState({data: data});
665
+ }.bind(this),
666
+ error: function(xhr, status, err) {
667
+ console.error(this.props.url, status, err.toString());
668
+ }.bind(this)
669
+ });
676
670
});
677
671
},
678
672
getInitialState: function() {
@@ -687,9 +681,7 @@ var CommentBox = React.createClass({
687
681
<div className="commentBox">
688
682
<h1>Comments</h1>
689
683
<CommentList data={this.state.data} />
690
- <CommentForm
691
- onCommentSubmit={this.handleCommentSubmit}
692
- />
684
+ <CommentForm onCommentSubmit={this.handleCommentSubmit} />
693
685
</div>
694
686
);
695
687
}
0 commit comments