@@ -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,10 +89,9 @@ The first thing you'll notice is the XML-ish syntax in your JavaScript. We have
91
89
var CommentBox = React .createClass ({
92
90
render : function () {
93
91
return (
94
- React .DOM .div ({
95
- className: ' commentBox' ,
96
- children: ' Hello, world! I am a CommentBox.'
97
- })
92
+ React .DOM .div ({className: " commentBox" },
93
+ " Hello, world! I am a CommentBox."
94
+ )
98
95
);
99
96
}
100
97
});
@@ -309,7 +306,11 @@ Now that the data is available in the `CommentList`, let's render the comments d
309
306
var CommentList = React.createClass({
310
307
render: function() {
311
308
var commentNodes = this.props.data.map(function (comment) {
312
- return <Comment author={comment.author}>{comment.text}</Comment>;
309
+ return (
310
+ <Comment author={comment.author}>
311
+ {comment.text}
312
+ </Comment>
313
+ );
313
314
});
314
315
return (
315
316
<div className="commentList">
@@ -492,11 +493,7 @@ var CommentForm = React.createClass({
492
493
return (
493
494
<form className="commentForm" onSubmit={this.handleSubmit}>
494
495
<input type="text" placeholder="Your name" ref="author" />
495
- <input
496
- type="text"
497
- placeholder="Say something..."
498
- ref="text"
499
- />
496
+ <input type="text" placeholder="Say something..." ref="text" />
500
497
<input type="submit" value="Post" />
501
498
</form>
502
499
);
@@ -550,9 +547,7 @@ var CommentBox = React.createClass({
550
547
<div className="commentBox">
551
548
<h1>Comments</h1>
552
549
<CommentList data={this.state.data} />
553
- <CommentForm
554
- onCommentSubmit={this.handleCommentSubmit}
555
- />
550
+ <CommentForm onCommentSubmit={this.handleCommentSubmit} />
556
551
</div>
557
552
);
558
553
}
@@ -576,11 +571,7 @@ var CommentForm = React.createClass({
576
571
return (
577
572
<form className="commentForm" onSubmit={this.handleSubmit}>
578
573
<input type="text" placeholder="Your name" ref="author" />
579
- <input
580
- type="text"
581
- placeholder="Say something..."
582
- ref="text"
583
- />
574
+ <input type="text" placeholder="Say something..." ref="text" />
584
575
<input type="submit" value="Post" />
585
576
</form>
586
577
);
@@ -631,9 +622,7 @@ var CommentBox = React.createClass({
631
622
<div className="commentBox">
632
623
<h1>Comments</h1>
633
624
<CommentList data={this.state.data} />
634
- <CommentForm
635
- onCommentSubmit={this.handleCommentSubmit}
636
- />
625
+ <CommentForm onCommentSubmit={this.handleCommentSubmit} />
637
626
</div>
638
627
);
639
628
}
@@ -662,18 +651,22 @@ var CommentBox = React.createClass({
662
651
handleCommentSubmit: function(comment) {
663
652
var comments = this.state.data;
664
653
var newComments = comments.concat([comment]);
665
- this.setState({data: newComments});
666
- $.ajax({
667
- url: this.props.url,
668
- dataType: 'json',
669
- type: 'POST',
670
- data: comment,
671
- success: function(data) {
672
- this.setState({data: data});
673
- }.bind(this),
674
- error: function(xhr, status, err) {
675
- console.error(this.props.url, status, err.toString());
676
- }.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
+ });
677
670
});
678
671
},
679
672
getInitialState: function() {
@@ -688,9 +681,7 @@ var CommentBox = React.createClass({
688
681
<div className="commentBox">
689
682
<h1>Comments</h1>
690
683
<CommentList data={this.state.data} />
691
- <CommentForm
692
- onCommentSubmit={this.handleCommentSubmit}
693
- />
684
+ <CommentForm onCommentSubmit={this.handleCommentSubmit} />
694
685
</div>
695
686
);
696
687
}
0 commit comments