Skip to content

Commit a541d39

Browse files
committed
add new demos
1 parent b018181 commit a541d39

File tree

20 files changed

+353
-202
lines changed

20 files changed

+353
-202
lines changed

README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Play with the source files under the repo's demo* directories.
3030
</html>
3131
```
3232

33-
## Render JSX
33+
## Demo1: Render JSX
3434

3535
React.render() translates JSX into HTML.
3636

@@ -41,7 +41,7 @@ React.render(
4141
);
4242
```
4343

44-
## Use JavaScript in JSX
44+
## Demo2: Use JavaScript in JSX
4545

4646
JSX takes angle brackets (beginning with &lt; ) as HTML section, curly brackets (beginning with { ) as JavaScript section.
4747

@@ -60,7 +60,7 @@ React.render(
6060
);
6161
```
6262

63-
## Use array in JSX
63+
## Demo3: Use array in JSX
6464

6565
JSX implicitly concats all members of an array into HTML.
6666

@@ -75,7 +75,7 @@ React.render(
7575
);
7676
```
7777

78-
## Define a component
78+
## Demo4: Define a component
7979

8080
React.createClass() defines a component which you could use in your pages.
8181

@@ -92,7 +92,7 @@ React.render(
9292
);
9393
```
9494

95-
## this.props.children
95+
## Demo5: this.props.children
9696

9797
React uses `this.props.children` to access a component's children.
9898

@@ -120,7 +120,7 @@ React.render(
120120
);
121121
```
122122

123-
## Finding a DOM node
123+
## Demo6: Finding a DOM node
124124

125125
React uses React.findDOMNode() to find a DOM node.
126126

@@ -145,7 +145,7 @@ React.render(
145145
);
146146
```
147147

148-
## this.state
148+
## Demo7: this.state
149149

150150
React thinks of component as state machines, and uses `this.state` to hold component's state, `this.setState()` to update `this.state` and re-render the component.
151151

@@ -173,9 +173,9 @@ React.render(
173173
);
174174
```
175175

176-
## Form
176+
## Demo8: Form
177177

178-
The `value` property of Form components, such as <input>, <textarea>, and <option>, is unaffected by any user input. If you wanted to update the value in response to user input, you could use the onChange event.
178+
The `value` property 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 update the value in response to user input, you could use the onChange event.
179179

180180
```js
181181
var Input = React.createClass({
@@ -199,7 +199,7 @@ var Input = React.createClass({
199199
React.render(<Input/>, document.body);
200200
```
201201

202-
## Component Lifecycle
202+
## Demo9: Component Lifecycle
203203

204204
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.
205205

@@ -239,7 +239,7 @@ React.render(
239239
);
240240
```
241241

242-
## Ajax
242+
## Demo10: Ajax
243243

244244
Fetch data in componentDidMount. When the response arrives, store the data in state, triggering a render to update your UI.
245245

@@ -284,13 +284,15 @@ React.render(
284284
285285
### Precompiling JSX
286286
287-
Install the command-line tools.
287+
All above demos don't use JSX compilation for clarity. In production environment, ensure to precompile JSX files before putting them online.
288+
289+
First, install the command-line tools.
288290

289291
```bash
290292
$ npm install -g react-tools
291293
```
292294

293-
Precompile your JSX files(.jsx) into JavaScript(.js).
295+
Then precompile your JSX files(.jsx) into JavaScript(.js).
294296

295297
```bash
296298
$ jsx -x src/ build/
@@ -317,6 +319,9 @@ Put the compiled JS files into HTML.
317319

318320
- [React's official site](http://facebook.github.io/react)
319321
- [React's official examples](https://github.com/facebook/react/tree/master/examples)
322+
- [React JS Tutorial and Guide to the Gotchas](https://zapier.com/engineering/react-js-tutorial-guide-gotchas/), by Justin Deal
323+
- [React Primer](https://github.com/BinaryMuse/react-primer), by Binary Muse
324+
- [jQuery versus React.js thinking](http://blog.zigomir.com/react.js/jquery/2015/01/11/jquery-versus-react-thinking.html), by zigomir
320325

321326
## License
322327

createClass/index.html

Lines changed: 0 additions & 22 deletions
This file was deleted.
File renamed without changes.

demo11/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// copied from https://github.com/mhart/react-server-example
2+
3+
$ npm install
4+
$ jsx src/ .
5+
$ node server.js

demo11/app.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var React = require('react');
2+
3+
module.exports = React.createClass({displayName: "exports",
4+
5+
getInitialState: function() {
6+
return {
7+
items: this.props.items,
8+
disabled: true
9+
}
10+
},
11+
12+
componentDidMount: function() {
13+
this.setState({
14+
disabled: false
15+
})
16+
},
17+
18+
handleClick: function() {
19+
this.setState({
20+
items: this.state.items.concat('Item ' + this.state.items.length)
21+
})
22+
},
23+
24+
render: function() {
25+
return (
26+
React.createElement("div", null,
27+
React.createElement("button", {onClick: this.handleClick, disabled: this.state.disabled}, "Add Item"),
28+
React.createElement("ul", null,
29+
30+
this.state.items.map(function(item) {
31+
return React.createElement("li", null, item)
32+
})
33+
34+
)
35+
)
36+
)
37+
},
38+
});

demo11/browser.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var React = require('react');
2+
var App = require('./app');
3+
4+
React.render(React.createElement(App, {items: window.APP_PROPS.items}), document.getElementById('content'));

demo11/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "react-server",
3+
"version": "1.0.0",
4+
"description": "an example of react.js in server",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "ruanyf",
10+
"license": "ISC",
11+
"dependencies": {
12+
"browserify": "~9.0.3",
13+
"literalify": "~0.4.0",
14+
"react": "~0.13.1"
15+
},
16+
"devDependencies": {
17+
"react-tools": "~0.13.1"
18+
}
19+
}

demo11/server.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var http = require('http'),
2+
browserify = require('browserify'),
3+
literalify = require('literalify'),
4+
React = require('react');
5+
6+
var App = require('./app');
7+
8+
http.createServer(function(req, res) {
9+
if (req.url == '/') {
10+
res.setHeader('Content-Type', 'text/html');
11+
var props = {
12+
items: [
13+
'Item 0',
14+
'Item 1'
15+
]
16+
};
17+
var html = React.renderToStaticMarkup(
18+
React.createElement("body", null,
19+
React.createElement("div", {id: "content", dangerouslySetInnerHTML: {__html:
20+
React.renderToString(React.createElement(App, {items: props.items}))
21+
}}), ",",
22+
23+
React.createElement("script", {dangerouslySetInnerHTML: {__html:
24+
'var APP_PROPS = ' + JSON.stringify(props) + ';'
25+
}}),
26+
React.createElement("script", {src: "//fb.me/react-0.13.1.min.js"}),
27+
React.createElement("script", {src: "/bundle.js"})
28+
)
29+
);
30+
res.end(html);
31+
32+
} else if (req.url == '/bundle.js') {
33+
res.setHeader('Content-Type', 'text/javascript');
34+
browserify()
35+
.add('./browser.js')
36+
.transform(literalify.configure({react: 'window.React'}))
37+
.bundle()
38+
.pipe(res);
39+
40+
} else {
41+
res.statusCode = 404;
42+
res.end();
43+
}
44+
}).listen(3000, function(err) {
45+
if (err) throw err;
46+
console.log('Listening on 3000...');
47+
})
48+

demo11/src/app.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var React = require('react');
2+
3+
module.exports = React.createClass({
4+
5+
getInitialState: function() {
6+
return {
7+
items: this.props.items,
8+
disabled: true
9+
}
10+
},
11+
12+
componentDidMount: function() {
13+
this.setState({
14+
disabled: false
15+
})
16+
},
17+
18+
handleClick: function() {
19+
this.setState({
20+
items: this.state.items.concat('Item ' + this.state.items.length)
21+
})
22+
},
23+
24+
render: function() {
25+
return (
26+
<div>
27+
<button onClick={this.handleClick} disabled={this.state.disabled}>Add Item</button>
28+
<ul>
29+
{
30+
this.state.items.map(function(item) {
31+
return <li>{item}</li>
32+
})
33+
}
34+
</ul>
35+
</div>
36+
)
37+
},
38+
});

demo11/src/browser.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var React = require('react');
2+
var App = require('./app');
3+
4+
React.render(<App items={window.APP_PROPS.items} />, document.getElementById('content'));

0 commit comments

Comments
 (0)