Skip to content

Commit 64cffaf

Browse files
committed
Update documentation for React 0.12
1 parent 74be336 commit 64cffaf

File tree

3 files changed

+53
-69
lines changed

3 files changed

+53
-69
lines changed

site/jekyll/getting-started/tutorial.md

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ React is all about modular, composable components. For our comment box example,
9090
Let's build the `CommentBox` component, which is just a simple `<div>`. Add this code to `Tutorial.jsx`:
9191

9292
```javascript
93-
/** @jsx React.DOM */
9493
var CommentBox = React.createClass({
9594
render: function() {
9695
return (
@@ -100,7 +99,7 @@ var CommentBox = React.createClass({
10099
);
101100
}
102101
});
103-
React.renderComponent(
102+
React.render(
104103
<CommentBox />,
105104
document.getElementById('content')
106105
);
@@ -117,19 +116,17 @@ If you see this, congratulations! You've just built your first React component.
117116
The first thing you'll notice is the XML-ish syntax in your JavaScript. We have a simple precompiler that translates the syntactic sugar to this plain JavaScript:
118117

119118
```javascript
120-
// tutorial1-raw.js
121-
var CommentBox = React.createClass({
119+
var CommentBox = React.createClass({displayName: 'CommentBox',
122120
render: function() {
123121
return (
124-
React.DOM.div({
125-
className: 'commentBox',
126-
children: 'Hello, world! I am a CommentBox.'
127-
})
122+
React.createElement('div', {className: "commentBox"},
123+
"Hello, world! I am a CommentBox."
124+
)
128125
);
129126
}
130127
});
131-
React.renderComponent(
132-
CommentBox({}),
128+
React.render(
129+
React.createElement(CommentBox, null),
133130
document.getElementById('content')
134131
);
135132
```
@@ -144,7 +141,7 @@ The `<div>` tags are not actual DOM nodes; they are instantiations of React `div
144141

145142
You do not have to return basic HTML. You can return a tree of components that you (or someone else) built. This is what makes React **composable**: a key tenet of maintainable frontends.
146143

147-
`React.renderComponent()` instantiates the root component, starts the framework, and injects the markup into a raw DOM element, provided as the second argument.
144+
`React.render()` instantiates the root component, starts the framework, and injects the markup into a raw DOM element, provided as the second argument.
148145

149146
## Composing components
150147

@@ -188,7 +185,7 @@ var CommentBox = React.createClass({
188185
});
189186
```
190187

191-
Notice how we're mixing HTML tags and components we've built. HTML components are regular React components, just like the ones you define, with one difference. The JSX compiler will automatically rewrite HTML tags to "React.DOM.tagName" expressions and leave everything else alone. This is to prevent the pollution of the global namespace.
188+
Notice how we're mixing HTML tags and components we've built. HTML components are regular React components, just like the ones you define, with one difference. The JSX compiler will automatically rewrite HTML tags to `React.createElement(tagName)` expressions and leave everything else alone. This is to prevent the pollution of the global namespace.
192189

193190
### Component Properties
194191

@@ -208,11 +205,11 @@ var CommentList = React.createClass({
208205
});
209206
```
210207

211-
Note that we have passed some data from the parent `CommentList` component to the child `Comment` component as both XML-like children and attributes. Data passed from parent to child is called **props**, short for properties.
208+
Note that we have passed some data from the parent `CommentList` component to the child `Comment` components. For example, we passed *Pete Hunt* (via an attribute) and *This is one comment* (via an XML-like child node) to the first `Comment`. Data passed from parent to children components is called **props**, short for properties.
212209

213210
### Using props
214211

215-
Let's create the Comment component. It will read the data passed to it from the CommentList and render some markup:
212+
Let's create the Comment component. Using **props** we will be able to read the data passed to it from the `CommentList`, and render some markup:
216213

217214
```javascript
218215
var Comment = React.createClass({
@@ -231,7 +228,6 @@ var Comment = React.createClass({
231228

232229
By surrounding a JavaScript expression in braces inside JSX (as either an attribute or child), you can drop text or React components into the tree. We access named attributes passed to the component as keys on `this.props` and any nested elements as `this.props.children`.
233230

234-
235231
### Adding Markdown
236232

237233
Markdown is a simple way to format your text inline. For example, surrounding text with asterisks will make it emphasized.
@@ -246,10 +242,10 @@ First, add the third-party **Showdown** library to your application. This is a J
246242

247243
Next, let's convert the comment text to Markdown and output it:
248244

249-
```javascript{1,9}
250-
var converter = new Showdown.converter();
245+
```javascript{3,9}
251246
var Comment = React.createClass({
252247
render: function() {
248+
var converter = new Showdown.converter();
253249
return (
254250
<div className="comment">
255251
<h2 className="commentAuthor">
@@ -269,9 +265,9 @@ But there's a problem! Our rendered comments look like this in the browser: "`<p
269265
That's React protecting you from an XSS attack. There's a way to get around it but the framework warns you not to use it:
270266

271267
```javascript{4,10}
272-
var converter = new Showdown.converter();
273268
var Comment = React.createClass({
274269
render: function() {
270+
var converter = new Showdown.converter();
275271
var rawMarkup = converter.makeHtml(this.props.children.toString());
276272
return (
277273
<div className="comment">
@@ -301,7 +297,7 @@ var data = [
301297
];
302298
```
303299

304-
We need to get this data into `CommentList` in a modular way. Modify `CommentBox` and the `renderComponent()` call to pass this data into the `CommentList` via props:
300+
We need to get this data into `CommentList` in a modular way. Modify `CommentBox` and the `React.render()` call to pass this data into the `CommentList` via props:
305301

306302
```javascript{6,14}
307303
var CommentBox = React.createClass({
@@ -316,19 +312,23 @@ var CommentBox = React.createClass({
316312
}
317313
});
318314
319-
React.renderComponent(
315+
React.render(
320316
<CommentBox data={data} />,
321317
document.getElementById('content')
322318
);
323319
```
324320

325321
Now that the data is available in the `CommentList`, let's render the comments dynamically:
326322

327-
```javascript{3-5,8}
323+
```javascript{3-8,12}
328324
var CommentList = React.createClass({
329325
render: function() {
330326
var commentNodes = this.props.data.map(function (comment) {
331-
return <Comment author={comment.Author}>{comment.Text}</Comment>;
327+
return (
328+
<Comment author={comment.author}>
329+
{comment.text}
330+
</Comment>
331+
);
332332
});
333333
return (
334334
<div className="commentList">
@@ -447,7 +447,7 @@ If you hit `/comments` in your browser, you should now see the data encoded as J
447447
Now that we have a data source, we can replace the hard-coded data with the dynamic data from the server. We will remove the data prop and replace it with a URL to fetch:
448448

449449
```javascript{2}
450-
React.renderComponent(
450+
React.render(
451451
<CommentBox url="/comments" />,
452452
document.getElementById('content')
453453
);
@@ -511,7 +511,7 @@ var CommentBox = React.createClass({
511511
});
512512
```
513513

514-
Here, `componentWillMount` is a method called automatically by React before a component is rendered. The key to dynamic updates is the call to `this.setState()`. We replace the old array of comments with the new one from the server and the UI automatically updates itself. Because of this reactivity, it is only a minor change to add live updates. We will use simple polling here but you could easily use [SignalR](http://signalr.net/) or other technologies.
514+
Here, `componentDidMount` is a method called automatically by React when a component is rendered. The key to dynamic updates is the call to `this.setState()`. We replace the old array of comments with the new one from the server and the UI automatically updates itself. Because of this reactivity, it is only a minor change to add live updates. We will use simple polling here but you could easily use [SignalR](http://signalr.net/) or other technologies.
515515

516516
```javascript{2,15-16,30}
517517
var CommentBox = React.createClass({
@@ -527,7 +527,7 @@ var CommentBox = React.createClass({
527527
getInitialState: function() {
528528
return {data: []};
529529
},
530-
componentWillMount: function() {
530+
componentDidMount: function() {
531531
this.loadCommentsFromServer();
532532
window.setInterval(this.loadCommentsFromServer, this.props.pollInterval);
533533
},
@@ -542,7 +542,7 @@ var CommentBox = React.createClass({
542542
}
543543
});
544544
545-
React.renderComponent(
545+
React.render(
546546
<CommentBox url="/comments" pollInterval={2000} />,
547547
document.getElementById('content')
548548
);
@@ -595,26 +595,23 @@ Let's make the form interactive. When the user submits the form, we should clear
595595

596596
```javascript{2-12,15-16,20}
597597
var CommentForm = React.createClass({
598-
handleSubmit: function() {
598+
handleSubmit: function(e) {
599+
e.preventDefault();
599600
var author = this.refs.author.getDOMNode().value.trim();
600601
var text = this.refs.text.getDOMNode().value.trim();
601602
if (!text || !author) {
602-
return false;
603+
return;
603604
}
604605
// TODO: send request to the server
605606
this.refs.author.getDOMNode().value = '';
606607
this.refs.text.getDOMNode().value = '';
607-
return false;
608+
return;
608609
},
609610
render: function() {
610611
return (
611612
<form className="commentForm" onSubmit={this.handleSubmit}>
612613
<input type="text" placeholder="Your name" ref="author" />
613-
<input
614-
type="text"
615-
placeholder="Say something..."
616-
ref="text"
617-
/>
614+
<input type="text" placeholder="Say something..." ref="text" />
618615
<input type="submit" value="Post" />
619616
</form>
620617
);
@@ -626,7 +623,7 @@ var CommentForm = React.createClass({
626623

627624
React attaches event handlers to components using a camelCase naming convention. We attach an `onSubmit` handler to the form that clears the form fields when the form is submitted with valid input.
628625

629-
We always return `false` from the event handler to prevent the browser's default action of submitting the form. (If you prefer, you can instead take the event as an argument and call `preventDefault()` on it.)
626+
Call `preventDefault()` on the event to prevent the browser's default action of submitting the form.
630627

631628
##### Refs
632629

@@ -638,7 +635,7 @@ When a user submits a comment, we will need to refresh the list of comments to i
638635

639636
We need to pass data from the child component to its parent. We do this by passing a `callback` in props from parent to child:
640637

641-
```javascript{11-13,27}
638+
```javascript{11-13,26}
642639
var CommentBox = React.createClass({
643640
loadCommentsFromServer: function() {
644641
var xhr = new XMLHttpRequest();
@@ -655,7 +652,7 @@ var CommentBox = React.createClass({
655652
getInitialState: function() {
656653
return {data: []};
657654
},
658-
componentWillMount: function() {
655+
componentDidMount: function() {
659656
this.loadCommentsFromServer();
660657
window.setInterval(this.loadCommentsFromServer, this.props.pollInterval);
661658
},
@@ -664,9 +661,7 @@ var CommentBox = React.createClass({
664661
<div className="commentBox">
665662
<h1>Comments</h1>
666663
<CommentList data={this.state.data} />
667-
<CommentForm
668-
onCommentSubmit={this.handleCommentSubmit}
669-
/>
664+
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
670665
</div>
671666
);
672667
}
@@ -675,28 +670,25 @@ var CommentBox = React.createClass({
675670

676671
Let's call the callback from the `CommentForm` when the user submits the form:
677672

678-
```javascript{8}
673+
```javascript{9}
679674
var CommentForm = React.createClass({
680-
handleSubmit: function() {
675+
handleSubmit: function(e) {
676+
e.preventDefault();
681677
var author = this.refs.author.getDOMNode().value.trim();
682678
var text = this.refs.text.getDOMNode().value.trim();
683679
if (!text || !author) {
684-
return false;
680+
return;
685681
}
686682
this.props.onCommentSubmit({author: author, text: text});
687683
this.refs.author.getDOMNode().value = '';
688684
this.refs.text.getDOMNode().value = '';
689-
return false;
685+
return;
690686
},
691687
render: function() {
692688
return (
693689
<form className="commentForm" onSubmit={this.handleSubmit}>
694690
<input type="text" placeholder="Your name" ref="author" />
695-
<input
696-
type="text"
697-
placeholder="Say something..."
698-
ref="text"
699-
/>
691+
<input type="text" placeholder="Say something..." ref="text" />
700692
<input type="submit" value="Post" />
701693
</form>
702694
);
@@ -706,7 +698,7 @@ var CommentForm = React.createClass({
706698

707699
Now that the callbacks are in place, all we have to do is submit to the server and refresh the list:
708700

709-
```javascript{12-21,44}
701+
```javascript{12-21,42}
710702
var CommentBox = React.createClass({
711703
loadCommentsFromServer: function() {
712704
var xhr = new XMLHttpRequest();
@@ -732,7 +724,7 @@ var CommentBox = React.createClass({
732724
getInitialState: function() {
733725
return {data: []};
734726
},
735-
componentWillMount: function() {
727+
componentDidMount: function() {
736728
this.loadCommentsFromServer();
737729
window.setInterval(this.loadCommentsFromServer, this.props.pollInterval);
738730
},
@@ -741,15 +733,13 @@ var CommentBox = React.createClass({
741733
<div className="commentBox">
742734
<h1>Comments</h1>
743735
<CommentList data={this.state.data} />
744-
<CommentForm
745-
onCommentSubmit={this.handleCommentSubmit}
746-
/>
736+
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
747737
</div>
748738
);
749739
}
750740
});
751741
752-
React.renderComponent(
742+
React.render(
753743
<CommentBox url="/comments" submitUrl="/comments/new" pollInterval={2000} />,
754744
document.getElementById('content')
755745
);
@@ -797,7 +787,7 @@ var CommentBox = React.createClass({
797787
getInitialState: function() {
798788
return {data: []};
799789
},
800-
componentWillMount: function() {
790+
componentDidMount: function() {
801791
this.loadCommentsFromServer();
802792
window.setInterval(this.loadCommentsFromServer, this.props.pollInterval);
803793
},
@@ -806,9 +796,7 @@ var CommentBox = React.createClass({
806796
<div className="commentBox">
807797
<h1>Comments</h1>
808798
<CommentList data={this.state.data} />
809-
<CommentForm
810-
onCommentSubmit={this.handleCommentSubmit}
811-
/>
799+
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
812800
</div>
813801
);
814802
}
@@ -880,9 +868,9 @@ If you go to this URL in your browser, you should notice that the code has been
880868

881869
Server-side rendering means that your application initially renders the components on the server-side, rather than fetching data from the server and rendering using JavaScript. This enhances the performance of your application since the user will see the initial state immediately.
882870

883-
We need to make some motifications to `CommentBox` to support server-side rendering. Firstly, we need to accept an `initialData` prop, which will be used to set the initial state of the component, rather than doing an AJAX request. We also need to ensure the `setInterval` call for polling for new comments is only executed client-side, by moving it to the `componentDidMount` method. Finally, we will remove the `loadCommentsFromServer` call from `getInitialState`, since it is no longer required.
871+
We need to make some motifications to `CommentBox` to support server-side rendering. Firstly, we need to accept an `initialData` prop, which will be used to set the initial state of the component, rather than doing an AJAX request. We also need to remove the `loadCommentsFromServer` call from `getInitialState`, since it is no longer required.
884872

885-
```javascript{28,30-32}
873+
```javascript{28}
886874
var CommentBox = React.createClass({
887875
loadCommentsFromServer: function() {
888876
var xhr = new XMLHttpRequest();
@@ -920,16 +908,14 @@ var CommentBox = React.createClass({
920908
<div className="commentBox">
921909
<h1>Comments</h1>
922910
<CommentList data={this.state.data} />
923-
<CommentForm
924-
onCommentSubmit={this.handleCommentSubmit}
925-
/>
911+
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
926912
</div>
927913
);
928914
}
929915
});
930916
```
931917

932-
In the view, we will accept the list of comments as the model, and use `Html.React` to render the component. This will replace the `React.renderComponent` call that currently exists in Tutorial.jsx. All the props from the current `React.renderComponent` call should be moved here, and the `React.renderComponent` call should be deleted.
918+
In the view, we will accept the list of comments as the model, and use `Html.React` to render the component. This will replace the `React.render` call that currently exists in Tutorial.jsx. All the props from the current `React.render` call should be moved here, and the `React.render` call should be deleted.
933919

934920
```html{1,10-16,20}
935921
@model IEnumerable<ReactDemo.Models.CommentModel>

site/jekyll/getting-started/usage.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ layout: docs
33
title: Basic Usage
44
---
55

6-
Once installed, create your React components as usual, ensuring you add the
7-
`/** @jsx React.DOM */` docblock.
6+
Once installed, create your React components as usual.
87

98
```javascript
109
// /Scripts/HelloWorld.jsx
11-
/** @jsx React.DOM */
1210
var HelloWorld = React.createClass({
1311
render: function () {
1412
return (

site/jekyll/guides/server-side-rendering.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ code.
6464

6565
<script src="http://fb.me/react-0.12.0.min.js"></script>
6666
<script src="/Scripts/HelloWorld.js"></script>
67-
<script>React.renderComponent(HelloWorld({"name":"Daniel"}), document.getElementById("react1"));</script>
67+
<script>React.render(HelloWorld({"name":"Daniel"}), document.getElementById("react1"));</script>
6868
```
6969

7070
The server-rendered HTML will automatically be reused by React client-side,

0 commit comments

Comments
 (0)