Skip to content

Commit 41c3d9c

Browse files
committed
Merge branch 'master' into new-docs
2 parents cb862e1 + 7b10b2b commit 41c3d9c

File tree

9 files changed

+105
-69
lines changed

9 files changed

+105
-69
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ React is a JavaScript library for building user interfaces.
1313
We have several examples [on the website](https://facebook.github.io/react/). Here is the first one to get you started:
1414

1515
```js
16-
var HelloMessage = React.createClass({
17-
render: function() {
16+
class HelloMessage extends React.Component {
17+
render() {
1818
return <div>Hello {this.props.name}</div>;
1919
}
20-
});
20+
}
2121

2222
ReactDOM.render(
2323
<HelloMessage name="John" />,

docs/Rakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ require('open-uri')
66
desc "download babel-browser"
77
task :fetch_remotes do
88
IO.copy_stream(
9-
open('https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js'),
10-
'js/babel-browser.min.js'
9+
open('https://unpkg.com/babel-[email protected]/babel.min.js'),
10+
'js/babel.min.js'
1111
)
1212
end
1313

docs/_js/examples/hello.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
var name = Math.random() > 0.5 ? 'Jane' : 'John';
12
var HELLO_COMPONENT = `
2-
var HelloMessage = React.createClass({
3-
render: function() {
3+
class HelloMessage extends React.Component {
4+
render() {
45
return <div>Hello {this.props.name}</div>;
56
}
6-
});
7+
}
78
8-
ReactDOM.render(<HelloMessage name="John" />, mountNode);
9-
`;
9+
ReactDOM.render(<HelloMessage name="${name}" />, mountNode);
10+
`.trim();
1011

1112
ReactDOM.render(
1213
<ReactPlayground codeText={HELLO_COMPONENT} />,

docs/_js/examples/markdown.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
var MARKDOWN_COMPONENT = `
2-
var MarkdownEditor = React.createClass({
3-
getInitialState: function() {
4-
return {value: 'Type some *markdown* here!'};
5-
},
6-
handleChange: function() {
2+
class MarkdownEditor extends React.Component {
3+
constructor(props) {
4+
super(props);
5+
this.handleChange = this.handleChange.bind(this);
6+
this.state = {value: 'Type some *markdown* here!'};
7+
}
8+
9+
handleChange() {
710
this.setState({value: this.refs.textarea.value});
8-
},
9-
rawMarkup: function() {
11+
}
12+
13+
getRawMarkup() {
1014
var md = new Remarkable();
1115
return { __html: md.render(this.state.value) };
12-
},
13-
render: function() {
16+
}
17+
18+
render() {
1419
return (
1520
<div className="MarkdownEditor">
1621
<h3>Input</h3>
@@ -21,15 +26,15 @@ var MarkdownEditor = React.createClass({
2126
<h3>Output</h3>
2227
<div
2328
className="content"
24-
dangerouslySetInnerHTML={this.rawMarkup()}
29+
dangerouslySetInnerHTML={this.getRawMarkup()}
2530
/>
2631
</div>
2732
);
2833
}
29-
});
34+
}
3035
3136
ReactDOM.render(<MarkdownEditor />, mountNode);
32-
`;
37+
`.trim();
3338

3439
ReactDOM.render(
3540
<ReactPlayground codeText={MARKDOWN_COMPONENT} />,

docs/_js/examples/timer.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
var TIMER_COMPONENT = `
2-
var Timer = React.createClass({
3-
getInitialState: function() {
4-
return {secondsElapsed: 0};
5-
},
6-
tick: function() {
7-
this.setState({secondsElapsed: this.state.secondsElapsed + 1});
8-
},
9-
componentDidMount: function() {
10-
this.interval = setInterval(this.tick, 1000);
11-
},
12-
componentWillUnmount: function() {
2+
class Timer extends React.Component {
3+
constructor(props) {
4+
super(props);
5+
this.state = {secondsElapsed: 0};
6+
}
7+
8+
tick() {
9+
this.setState((prevState) => ({
10+
secondsElapsed: prevState.secondsElapsed + 1
11+
}));
12+
}
13+
14+
componentDidMount() {
15+
this.interval = setInterval(() => this.tick(), 1000);
16+
}
17+
18+
componentWillUnmount() {
1319
clearInterval(this.interval);
14-
},
15-
render: function() {
20+
}
21+
22+
render() {
1623
return (
1724
<div>Seconds Elapsed: {this.state.secondsElapsed}</div>
1825
);
1926
}
20-
});
27+
}
2128
2229
ReactDOM.render(<Timer />, mountNode);
23-
`;
30+
`.trim();
2431

2532
ReactDOM.render(
2633
<ReactPlayground codeText={TIMER_COMPONENT} />,

docs/_js/examples/todo.js

+39-24
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
11
var TODO_COMPONENT = `
2-
var TodoList = React.createClass({
3-
render: function() {
4-
var createItem = function(item) {
5-
return <li key={item.id}>{item.text}</li>;
6-
};
7-
return <ul>{this.props.items.map(createItem)}</ul>;
2+
class TodoApp extends React.Component {
3+
constructor(props) {
4+
super(props);
5+
this.handleChange = this.handleChange.bind(this);
6+
this.handleSubmit = this.handleSubmit.bind(this);
7+
this.state = {items: [], text: ''};
88
}
9-
});
10-
var TodoApp = React.createClass({
11-
getInitialState: function() {
12-
return {items: [], text: ''};
13-
},
14-
onChange: function(e) {
15-
this.setState({text: e.target.value});
16-
},
17-
handleSubmit: function(e) {
18-
e.preventDefault();
19-
var nextItems = this.state.items.concat([{text: this.state.text, id: Date.now()}]);
20-
var nextText = '';
21-
this.setState({items: nextItems, text: nextText});
22-
},
23-
render: function() {
9+
10+
render() {
2411
return (
2512
<div>
2613
<h3>TODO</h3>
2714
<TodoList items={this.state.items} />
2815
<form onSubmit={this.handleSubmit}>
29-
<input onChange={this.onChange} value={this.state.text} />
16+
<input onChange={this.handleChange} value={this.state.text} />
3017
<button>{'Add #' + (this.state.items.length + 1)}</button>
3118
</form>
3219
</div>
3320
);
3421
}
35-
});
22+
23+
handleChange(e) {
24+
this.setState({text: e.target.value});
25+
}
26+
27+
handleSubmit(e) {
28+
e.preventDefault();
29+
var newItem = {
30+
text: this.state.text,
31+
id: Date.now()
32+
};
33+
this.setState((prevState) => ({
34+
items: prevState.items.concat(newItem),
35+
text: ''
36+
}));
37+
}
38+
}
39+
40+
class TodoList extends React.Component {
41+
render() {
42+
return (
43+
<ul>
44+
{this.props.items.map(item => (
45+
<li key={item.id}>{item.text}</li>
46+
))}
47+
</ul>
48+
);
49+
}
50+
}
3651
3752
ReactDOM.render(<TodoApp />, mountNode);
38-
`;
53+
`.trim();
3954

4055
ReactDOM.render(
4156
<ReactPlayground codeText={TODO_COMPONENT} />,

docs/_js/live_editor.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,14 @@ var ReactPlayground = React.createClass({
9090

9191
getDefaultProps: function() {
9292
return {
93-
transformer: function(code) {
94-
return babel.transform(code).code;
93+
transformer: function(code, options) {
94+
var presets = ['react'];
95+
if (!options || !options.skipES2015Transform) {
96+
presets.push('es2015');
97+
}
98+
return Babel.transform(code, {
99+
presets
100+
}).code;
95101
},
96102
editorTabTitle: 'Live JSX Editor',
97103
showCompiledJSTab: true,
@@ -115,15 +121,15 @@ var ReactPlayground = React.createClass({
115121
this.setState({mode: mode});
116122
},
117123

118-
compileCode: function() {
119-
return this.props.transformer(this.state.code);
124+
compileCode: function(options) {
125+
return this.props.transformer(this.state.code, options);
120126
},
121127

122128
render: function() {
123129
var isJS = this.state.mode === this.MODES.JS;
124130
var compiledCode = '';
125131
try {
126-
compiledCode = this.compileCode();
132+
compiledCode = this.compileCode({skipES2015Transform: true});
127133
} catch (err) {}
128134

129135
var JSContent =
@@ -201,13 +207,15 @@ var ReactPlayground = React.createClass({
201207
} catch (e) { }
202208

203209
try {
204-
var compiledCode = this.compileCode();
210+
var compiledCode;
205211
if (this.props.renderCode) {
212+
compiledCode = this.compileCode({skipES2015Transform: true});
206213
ReactDOM.render(
207214
<CodeMirrorEditor codeText={compiledCode} readOnly={true} />,
208215
mountNode
209216
);
210217
} else {
218+
compiledCode = this.compileCode({skipES2015Transform: false});
211219
eval(compiledCode);
212220
}
213221
} catch (err) {

docs/_layouts/default.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<script src="/react/js/jsx.js"></script>
3737
<script src="/react/js/react.js"></script>
3838
<script src="/react/js/react-dom.js"></script>
39-
<script src="/react/js/babel-browser.min.js"></script>
39+
<script src="/react/js/babel.min.js"></script>
4040
<script src="/react/js/live_editor.js"></script>
4141
</head>
4242
<body>

docs/docs-old/05-reusable-components.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ With `React.createClass()`, you have to provide a separate `getInitialState` met
324324
```javascript
325325
var Counter = React.createClass({
326326
getInitialState: function() {
327-
return {count: props.initialCount};
327+
return {count: this.props.initialCount};
328328
},
329329
// ...
330330
});

0 commit comments

Comments
 (0)