@@ -22,7 +22,7 @@ It'll also have a few neat features:
2222
2323### Want to skip all this and just see the source?
2424
25- [ It's all on GitHub.] ( https://github.com/petehunt /react-tutorial )
25+ [ It's all on GitHub.] ( https://github.com/reactjs /react-tutorial )
2626
2727### Getting started
2828
@@ -40,9 +40,7 @@ For this tutorial we'll use prebuilt JavaScript files on a CDN. Open up your fav
4040 <body >
4141 <div id =" content" ></div >
4242 <script type =" text/jsx" >
43- /* *
44- * @jsx React.DOM
45- */
43+ /* * @jsx React.DOM */
4644 // The above declaration must remain intact at the top of the script.
4745 // Your code here
4846 </script >
@@ -91,7 +89,7 @@ The first thing you'll notice is the XML-ish syntax in your JavaScript. We have
9189var CommentBox = React .createClass ({displayName: ' CommentBox' ,
9290 render : function () {
9391 return (
94- React .DOM .div ({className: " commentBox" },
92+ React .DOM .div ({className: " commentBox" },
9593 " Hello, world! I am a CommentBox."
9694 )
9795 );
@@ -308,7 +306,11 @@ Now that the data is available in the `CommentList`, let's render the comments d
308306var CommentList = React.createClass({
309307 render: function() {
310308 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+ );
312314 });
313315 return (
314316 <div className="commentList">
@@ -491,11 +493,7 @@ var CommentForm = React.createClass({
491493 return (
492494 <form className="commentForm" onSubmit={this.handleSubmit}>
493495 <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" />
499497 <input type="submit" value="Post" />
500498 </form>
501499 );
@@ -549,9 +547,7 @@ var CommentBox = React.createClass({
549547 <div className="commentBox">
550548 <h1>Comments</h1>
551549 <CommentList data={this.state.data} />
552- <CommentForm
553- onCommentSubmit={this.handleCommentSubmit}
554- />
550+ <CommentForm onCommentSubmit={this.handleCommentSubmit} />
555551 </div>
556552 );
557553 }
@@ -575,11 +571,7 @@ var CommentForm = React.createClass({
575571 return (
576572 <form className="commentForm" onSubmit={this.handleSubmit}>
577573 <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" />
583575 <input type="submit" value="Post" />
584576 </form>
585577 );
@@ -630,9 +622,7 @@ var CommentBox = React.createClass({
630622 <div className="commentBox">
631623 <h1>Comments</h1>
632624 <CommentList data={this.state.data} />
633- <CommentForm
634- onCommentSubmit={this.handleCommentSubmit}
635- />
625+ <CommentForm onCommentSubmit={this.handleCommentSubmit} />
636626 </div>
637627 );
638628 }
@@ -661,18 +651,22 @@ var CommentBox = React.createClass({
661651 handleCommentSubmit: function(comment) {
662652 var comments = this.state.data;
663653 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+ });
676670 });
677671 },
678672 getInitialState: function() {
@@ -687,9 +681,7 @@ var CommentBox = React.createClass({
687681 <div className="commentBox">
688682 <h1>Comments</h1>
689683 <CommentList data={this.state.data} />
690- <CommentForm
691- onCommentSubmit={this.handleCommentSubmit}
692- />
684+ <CommentForm onCommentSubmit={this.handleCommentSubmit} />
693685 </div>
694686 );
695687 }
0 commit comments