Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit acba66f

Browse files
committed
[Docs] Put tutorial up-to-date with the code
(cherry picked from commit 58a463f) Conflicts: docs/docs/tutorial.md
1 parent 661bafb commit acba66f

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

docs/docs/tutorial.md

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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,10 +89,9 @@ The first thing you'll notice is the XML-ish syntax in your JavaScript. We have
9189
var CommentBox = React.createClass({
9290
render: function() {
9391
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+
)
9895
);
9996
}
10097
});
@@ -309,7 +306,11 @@ Now that the data is available in the `CommentList`, let's render the comments d
309306
var CommentList = React.createClass({
310307
render: function() {
311308
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+
);
313314
});
314315
return (
315316
<div className="commentList">
@@ -492,11 +493,7 @@ var CommentForm = React.createClass({
492493
return (
493494
<form className="commentForm" onSubmit={this.handleSubmit}>
494495
<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" />
500497
<input type="submit" value="Post" />
501498
</form>
502499
);
@@ -550,9 +547,7 @@ var CommentBox = React.createClass({
550547
<div className="commentBox">
551548
<h1>Comments</h1>
552549
<CommentList data={this.state.data} />
553-
<CommentForm
554-
onCommentSubmit={this.handleCommentSubmit}
555-
/>
550+
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
556551
</div>
557552
);
558553
}
@@ -576,11 +571,7 @@ var CommentForm = React.createClass({
576571
return (
577572
<form className="commentForm" onSubmit={this.handleSubmit}>
578573
<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" />
584575
<input type="submit" value="Post" />
585576
</form>
586577
);
@@ -631,9 +622,7 @@ var CommentBox = React.createClass({
631622
<div className="commentBox">
632623
<h1>Comments</h1>
633624
<CommentList data={this.state.data} />
634-
<CommentForm
635-
onCommentSubmit={this.handleCommentSubmit}
636-
/>
625+
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
637626
</div>
638627
);
639628
}
@@ -662,18 +651,22 @@ var CommentBox = React.createClass({
662651
handleCommentSubmit: function(comment) {
663652
var comments = this.state.data;
664653
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+
});
677670
});
678671
},
679672
getInitialState: function() {
@@ -688,9 +681,7 @@ var CommentBox = React.createClass({
688681
<div className="commentBox">
689682
<h1>Comments</h1>
690683
<CommentList data={this.state.data} />
691-
<CommentForm
692-
onCommentSubmit={this.handleCommentSubmit}
693-
/>
684+
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
694685
</div>
695686
);
696687
}

0 commit comments

Comments
 (0)