You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: site/jekyll/getting-started/tutorial.md
+51-65Lines changed: 51 additions & 65 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,7 +90,6 @@ React is all about modular, composable components. For our comment box example,
90
90
Let's build the `CommentBox` component, which is just a simple `<div>`. Add this code to `Tutorial.jsx`:
91
91
92
92
```javascript
93
-
/** @jsx React.DOM */
94
93
var CommentBox =React.createClass({
95
94
render:function() {
96
95
return (
@@ -100,7 +99,7 @@ var CommentBox = React.createClass({
100
99
);
101
100
}
102
101
});
103
-
React.renderComponent(
102
+
React.render(
104
103
<CommentBox />,
105
104
document.getElementById('content')
106
105
);
@@ -117,19 +116,17 @@ If you see this, congratulations! You've just built your first React component.
117
116
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:
118
117
119
118
```javascript
120
-
// tutorial1-raw.js
121
-
var CommentBox =React.createClass({
119
+
var CommentBox =React.createClass({displayName:'CommentBox',
@@ -144,7 +141,7 @@ The `<div>` tags are not actual DOM nodes; they are instantiations of React `div
144
141
145
142
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.
146
143
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.
148
145
149
146
## Composing components
150
147
@@ -188,7 +185,7 @@ var CommentBox = React.createClass({
188
185
});
189
186
```
190
187
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.
192
189
193
190
### Component Properties
194
191
@@ -208,11 +205,11 @@ var CommentList = React.createClass({
208
205
});
209
206
```
210
207
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.
212
209
213
210
### Using props
214
211
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:
216
213
217
214
```javascript
218
215
varComment=React.createClass({
@@ -231,7 +228,6 @@ var Comment = React.createClass({
231
228
232
229
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`.
233
230
234
-
235
231
### Adding Markdown
236
232
237
233
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
246
242
247
243
Next, let's convert the comment text to Markdown and output it:
248
244
249
-
```javascript{1,9}
250
-
var converter = new Showdown.converter();
245
+
```javascript{3,9}
251
246
var Comment = React.createClass({
252
247
render: function() {
248
+
var converter = new Showdown.converter();
253
249
return (
254
250
<div className="comment">
255
251
<h2 className="commentAuthor">
@@ -269,9 +265,9 @@ But there's a problem! Our rendered comments look like this in the browser: "`<p
269
265
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:
270
266
271
267
```javascript{4,10}
272
-
var converter = new Showdown.converter();
273
268
var Comment = React.createClass({
274
269
render: function() {
270
+
var converter = new Showdown.converter();
275
271
var rawMarkup = converter.makeHtml(this.props.children.toString());
276
272
return (
277
273
<div className="comment">
@@ -301,7 +297,7 @@ var data = [
301
297
];
302
298
```
303
299
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:
305
301
306
302
```javascript{6,14}
307
303
var CommentBox = React.createClass({
@@ -316,19 +312,23 @@ var CommentBox = React.createClass({
316
312
}
317
313
});
318
314
319
-
React.renderComponent(
315
+
React.render(
320
316
<CommentBox data={data} />,
321
317
document.getElementById('content')
322
318
);
323
319
```
324
320
325
321
Now that the data is available in the `CommentList`, let's render the comments dynamically:
326
322
327
-
```javascript{3-5,8}
323
+
```javascript{3-8,12}
328
324
var CommentList = React.createClass({
329
325
render: function() {
330
326
var commentNodes = this.props.data.map(function (comment) {
@@ -447,7 +447,7 @@ If you hit `/comments` in your browser, you should now see the data encoded as J
447
447
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:
448
448
449
449
```javascript{2}
450
-
React.renderComponent(
450
+
React.render(
451
451
<CommentBox url="/comments" />,
452
452
document.getElementById('content')
453
453
);
@@ -511,7 +511,7 @@ var CommentBox = React.createClass({
511
511
});
512
512
```
513
513
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.
515
515
516
516
```javascript{2,15-16,30}
517
517
var CommentBox = React.createClass({
@@ -527,7 +527,7 @@ var CommentBox = React.createClass({
@@ -626,7 +623,7 @@ var CommentForm = React.createClass({
626
623
627
624
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.
628
625
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.
630
627
631
628
##### Refs
632
629
@@ -638,7 +635,7 @@ When a user submits a comment, we will need to refresh the list of comments to i
638
635
639
636
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:
640
637
641
-
```javascript{11-13,27}
638
+
```javascript{11-13,26}
642
639
var CommentBox = React.createClass({
643
640
loadCommentsFromServer: function() {
644
641
var xhr = new XMLHttpRequest();
@@ -655,7 +652,7 @@ var CommentBox = React.createClass({
@@ -880,9 +868,9 @@ If you go to this URL in your browser, you should notice that the code has been
880
868
881
869
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.
882
870
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.
884
872
885
-
```javascript{28,30-32}
873
+
```javascript{28}
886
874
var CommentBox = React.createClass({
887
875
loadCommentsFromServer: function() {
888
876
var xhr = new XMLHttpRequest();
@@ -920,16 +908,14 @@ var CommentBox = React.createClass({
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.
0 commit comments