Skip to content

Commit 79ef02f

Browse files
committed
edit README
1 parent 5393693 commit 79ef02f

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

README.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ These demos are purposely written in a simple and clear style. You will find no
1010
4. [Define a component](#demo04-define-a-component)
1111
5. [this.props.children](#demo05-thispropschildren)
1212
6. [Finding a DOM node](#demo6-finding-a-dom-node)
13-
7. this.state
14-
8. Form
15-
9. Component Lifecycle
16-
10. Ajax
17-
11. Server-side rendering
13+
7. [this.state](#demo07-thisstate)
14+
8. [Form](#demo08-form)
15+
9. [Component Lifecycle](#demo09-component-lifecycle)
16+
10. [Ajax](#demo10-ajax)
17+
11. [Server-side rendering](#demo11-server-side-rendering)
1818

1919
---
2020

@@ -46,7 +46,7 @@ Then play with the source files under the repo's demo* directories.
4646
</html>
4747
```
4848

49-
## Demo01: Render JSX
49+
## [Demo01: Render JSX](https://github.com/ruanyf/react-demos/blob/master/demo01/index.html)
5050

5151
The template syntax in React is called [JSX](http://facebook.github.io/react/docs/displaying-data.html#jsx-syntax). It is allowed in JSX to put HTML tags directly into JavaScript codes. React.render() is the method which translates JSX into HTML, and renders it into the specified DOM node.
5252

@@ -171,7 +171,7 @@ The desired DOM node should have a `ref` attribute, and `React.findDOMNode(this.
171171

172172
## Demo07: this.state
173173

174-
React thinks of component as state machines, and uses `this.state` to hold component's state, `getInitialState()` to initialize `this.state`, `this.setState()` to update `this.state` and re-render the component.
174+
React thinks of component as state machines, and uses `this.state` to hold component's state, `getInitialState()` to initialize `this.state`(invoked before a component is mounted), `this.setState()` to update `this.state` and re-render the component.
175175

176176
```js
177177
var LikeButton = React.createClass({
@@ -203,7 +203,7 @@ You could use component attributes to register event handlers, just like `onClic
203203

204204
According to React's design philosophy, `this.state` describes the state of component and is mutated via user interactions, and `this.props` describes the properties of component and is stable adn immutable.
205205

206-
Since that, the `value` attribute of Form components, such as &lt;input$gt;, &lt;textarea$gt;, and &lt;option$gt;, is unaffected by any user input. If you wanted to access or update the value in response to user input, you could use the onChange event.
206+
Since that, the `value` attribute of Form components, such as &lt;input&gt;, &lt;textarea&gt;, and &lt;option&gt;, is unaffected by any user input. If you wanted to access or update the value in response to user input, you could use the onChange event.
207207

208208
```js
209209
var Input = React.createClass({
@@ -231,7 +231,7 @@ More information on [officail document](http://facebook.github.io/react/docs/for
231231

232232
## Demo09: Component Lifecycle
233233

234-
Components have three main parts of their lifecycle: Mounting, Updating and Unmounting. React provides hooks into these lifecycle part. `will` methods are called right before something happens, and `did` methods which are called right after something happens.
234+
Components have three main parts of [their lifecycle](https://facebook.github.io/react/docs/working-with-the-browser.html#component-lifecycle): Mounting(being inserted into the DOM), Updating(being re-rendered) and Unmounting(being removed from the DOM). React provides hooks into these lifecycle part. `will` methods are called right before something happens, and `did` methods which are called right after something happens.
235235

236236
```js
237237
var Hello = React.createClass({
@@ -269,9 +269,19 @@ React.render(
269269
);
270270
```
271271

272+
The following is [a whole list of lifecycle methods](http://facebook.github.io/react/docs/component-specs.html#lifecycle-methods).
273+
274+
- componentWillMount()
275+
- componentDidMount()
276+
- componentWillUpdate(object nextProps, object nextState)
277+
- componentDidUpdate(object prevProps, object prevState)
278+
- componentWillUnmount()
279+
- componentWillReceiveProps(object nextProps): invoked when a mounted component receives new props.
280+
- shouldComponentUpdate(object nextProps, object nextState): invoked when a component decides whether any changes warrant an update to the DOM.
281+
272282
## Demo10: Ajax
273283

274-
Fetch data in componentDidMount. When the response arrives, store the data in state, triggering a render to update your UI.
284+
How to get the data of a component from a server or an API provider? The answer is using Ajax to fetch data in the event handler of `componentDidMount`. When the server response arrives, store the data with `this.setState()` to trigger a re-render of your UI.
275285

276286
```js
277287
var UserGist = React.createClass({
@@ -312,7 +322,18 @@ React.render(
312322
313323
## Demo11: Server-side rendering
314324
315-
Please visit [here](https://github.com/mhart/react-server-example).
325+
This demo is copied from [github.com/mhart/react-server-example](https://github.com/mhart/react-server-example), but I rewrote it with JSX syntax.
326+
327+
```bash
328+
# install the dependencies in demo11 directory
329+
$ npm install
330+
331+
# translate all jsx file in src subdirectory to js file
332+
$ jsx src/ .
333+
334+
# launch http server
335+
$ node server.js
336+
```
316337
317338
## Extras
318339

demo11/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
// copied from https://github.com/mhart/react-server-examplei
1+
This demo is copied from [github.com/mhart/react-server-example](https://github.com/mhart/react-server-example), but I rewrote it with JSX syntax.
22

3-
$ npm install
4-
$ jsx src/ .
5-
$ node server.js
3+
```bash
4+
# install the dependencies in demo11 directory
5+
$ npm install
6+
7+
# translate all jsx file in src subdirectory to js file
8+
$ jsx src/ .
69

10+
# launch http server
11+
$ node server.js
12+
```

0 commit comments

Comments
 (0)