├── .gitignore
├── demo01
├── js
│ └── app.jsx
└── index.html
├── demo04
├── js
│ └── app.jsx
└── index.html
├── demo03
├── js
│ └── app.jsx
└── index.html
├── demo13
├── src
│ ├── browser.js
│ ├── app.js
│ └── server.js
├── README.md
├── package.json
├── browser.js
├── server.js
└── app.js
├── demo02
├── js
│ └── app.jsx
└── index.html
├── demo06
├── js
│ └── app.jsx
└── index.html
├── demo05
├── index.html
└── js
│ └── app.jsx
├── demo07
├── index.html
└── js
│ └── app.jsx
├── demo08
├── index.html
└── js
│ └── app.jsx
├── demo09
├── index.html
└── js
│ └── app.jsx
├── demo10
├── index.html
└── js
│ └── app.jsx
├── demo11
├── index.html
└── js
│ └── app.jsx
├── demo12
├── index.html
└── js
│ └── app.jsx
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | example/
2 | node_modules
3 |
--------------------------------------------------------------------------------
/demo01/js/app.jsx:
--------------------------------------------------------------------------------
1 | ReactDOM.render(
2 |
{this.handleClick(event)}}>
17 | You {text} this. Click to toggle.
18 |
19 | );
20 | }
21 | }
22 | ReactDOM.render(
23 | )
22 | }} />
23 |
24 |
27 |
28 |
29 |
30 |
31 | );
32 | res.end(html);
33 |
34 | } else if (req.url == '/bundle.js') {
35 | res.setHeader('Content-Type', 'text/javascript');
36 | browserify()
37 | .add('./browser.js')
38 | .transform(literalify.configure({
39 | 'react': 'window.React',
40 | 'react-dom': 'window.ReactDOM',
41 | }))
42 | .bundle()
43 | .pipe(res);
44 | } else {
45 | res.statusCode = 404;
46 | res.end();
47 | }
48 | }).listen(3000, function(err) {
49 | if (err) throw err;
50 | console.log('Listening on 3000...');
51 | })
52 |
53 |
--------------------------------------------------------------------------------
/demo13/server.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var http = require('http'),
4 | browserify = require('browserify'),
5 | literalify = require('literalify'),
6 | React = require('react'),
7 | ReactDOMServer = require('react-dom/server');
8 |
9 | var App = require('./app');
10 |
11 | http.createServer(function (req, res) {
12 | if (req.url == '/') {
13 | res.setHeader('Content-Type', 'text/html');
14 | var props = {
15 | items: ['Item 0', 'Item 1']
16 | };
17 | var html = ReactDOMServer.renderToStaticMarkup(React.createElement(
18 | 'body',
19 | null,
20 | React.createElement('div', { id: 'content', dangerouslySetInnerHTML: { __html: ReactDOMServer.renderToString(React.createElement(App, { items: props.items }))
21 | } }),
22 | React.createElement('script', { dangerouslySetInnerHTML: { __html: 'var APP_PROPS = ' + JSON.stringify(props) + ';'
23 | } }),
24 | React.createElement('script', { src: 'http://cdn.bootcss.com/react/0.14.2/react.min.js' }),
25 | React.createElement('script', { src: 'http://cdn.bootcss.com/react/0.14.2/react-dom.min.js' }),
26 | React.createElement('script', { src: '/bundle.js' })
27 | ));
28 | res.end(html);
29 | } else if (req.url == '/bundle.js') {
30 | res.setHeader('Content-Type', 'text/javascript');
31 | browserify().add('./browser.js').transform(literalify.configure({
32 | 'react': 'window.React',
33 | 'react-dom': 'window.ReactDOM'
34 | })).bundle().pipe(res);
35 | } else {
36 | res.statusCode = 404;
37 | res.end();
38 | }
39 | }).listen(3000, function (err) {
40 | if (err) throw err;
41 | console.log('Listening on 3000...');
42 | });
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React ES6 Version
2 | This is a collection of simple demos of React.js for using ES6.
3 |
4 | 这是一个React使用ES6语法的简易教程,这个实例教程在阮一峰老师的[React入门实例教程](http://www.ruanyifeng.com/blog/2015/03/react.html)的基础上使用ES6重写而来
5 |
6 | [origin github website](https://github.com/ruanyf/react-demos)
7 |
8 | [原github地址](https://github.com/ruanyf/react-demos)
9 | >你只需要跟着每一个 Demo 做一遍,就能初步掌握 React 。当然,前提是你必须拥有基本 JavaScript 和 DOM 知识,但是你读完就会发现,React 所要求的预备知识真的很少。
10 |
11 | 
12 | # How to use
13 | ```
14 | $ git clone git@github.com:zhuangtongfa/react-demos.git
15 | ```
16 | use `http-server` in the folder,open the '127.0.0.1:8080'
17 |
18 | # How to install http-server
19 | ```
20 | npm install http-server -g
21 | ```
22 |
23 | # HTML Template
24 | ```html
25 |
26 |
27 |
28 |
29 |
Document
30 |
31 |
32 |
33 |
35 |
36 |
37 |
38 |
39 |
40 |
41 | ```
42 | # Class
43 | ```js
44 | //ES5
45 | var NotesList = React.createClass({
46 | render: function() {
47 | return (
48 |
49 | {
50 | React.Children.map(this.props.children, function (child) {
51 | return - {child}
;
52 | })
53 | }
54 |
55 | );
56 | }
57 | });
58 | ```
59 |
60 | ```js
61 | //ES6
62 | class NotesList extends React.Component{
63 | render(){
64 | return (
65 |
66 | {
67 | React.Children.map(this.props.children, function (child) {
68 | return - {child}
;
69 | })
70 | }
71 |
72 | );
73 | }
74 | }
75 | ```
76 | # State
77 | ```js
78 | //ES5
79 | getInitialState: function() {
80 | return {liked: false};
81 | }
82 | ```
83 |
84 | ```js
85 | //ES6
86 | constructor(props) {
87 | super(props);
88 | this.state= {
89 | liked:false
90 | }
91 | }
92 | ```
93 | # isMounted
94 | ES6 is no support `this.isMounted()`
95 |
96 | # Event
97 | ```js
98 | //ES5
99 |
100 | ```
101 | ```js
102 | //ES6
103 |
{this.handleChange(e)}} />
104 | ```
105 |
106 | # This is also hava a React-webpack-ES6 demo
107 | [github](https://github.com/zhuangtongfa/react-webpack-ES6-demo)
108 |
109 | Try it!
110 |
--------------------------------------------------------------------------------
/demo13/app.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | Object.defineProperty(exports, '__esModule', {
4 | value: true
5 | });
6 |
7 | var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
8 |
9 | var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
10 |
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
12 |
13 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
14 |
15 | function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
16 |
17 | var _react = require('react');
18 |
19 | var _react2 = _interopRequireDefault(_react);
20 |
21 | var App = (function (_React$Component) {
22 | _inherits(App, _React$Component);
23 |
24 | function App(props) {
25 | _classCallCheck(this, App);
26 |
27 | _get(Object.getPrototypeOf(App.prototype), 'constructor', this).call(this, props);
28 | this.render = this.render.bind(this);
29 | this.state = {
30 | items: this.props.items,
31 | disabled: true
32 | };
33 | }
34 |
35 | _createClass(App, [{
36 | key: 'componentDidMount',
37 | value: function componentDidMount() {
38 | this.setState({
39 | disabled: false
40 | });
41 | }
42 | }, {
43 | key: 'handleClick',
44 | value: function handleClick() {
45 | this.setState({
46 | items: this.state.items.concat('Item ' + this.state.items.length)
47 | });
48 | }
49 | }, {
50 | key: 'render',
51 | value: function render() {
52 | return _react2['default'].createElement(
53 | 'div',
54 | null,
55 | _react2['default'].createElement(
56 | 'button',
57 | { onClick: this.handleClick.bind(this), disabled: this.state.disabled },
58 | 'Add Item'
59 | ),
60 | _react2['default'].createElement(
61 | 'ul',
62 | null,
63 | this.state.items.map(function (item) {
64 | return _react2['default'].createElement(
65 | 'li',
66 | null,
67 | item
68 | );
69 | })
70 | )
71 | );
72 | }
73 | }]);
74 |
75 | return App;
76 | })(_react2['default'].Component);
77 |
78 | exports['default'] = App;
79 | ;
80 | module.exports = exports['default'];
--------------------------------------------------------------------------------