Update the homepage with ES6 (#7868)
* Update the homepage with ES6 * Avoid array spread and stale state
This commit is contained in:
parent
c9ec4bc445
commit
7b10b2b88d
|
@ -13,11 +13,11 @@ React is a JavaScript library for building user interfaces.
|
||||||
We have several examples [on the website](https://facebook.github.io/react/). Here is the first one to get you started:
|
We have several examples [on the website](https://facebook.github.io/react/). Here is the first one to get you started:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var HelloMessage = React.createClass({
|
class HelloMessage extends React.Component {
|
||||||
render: function() {
|
render() {
|
||||||
return <div>Hello {this.props.name}</div>;
|
return <div>Hello {this.props.name}</div>;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<HelloMessage name="John" />,
|
<HelloMessage name="John" />,
|
||||||
|
|
|
@ -6,8 +6,8 @@ require('open-uri')
|
||||||
desc "download babel-browser"
|
desc "download babel-browser"
|
||||||
task :fetch_remotes do
|
task :fetch_remotes do
|
||||||
IO.copy_stream(
|
IO.copy_stream(
|
||||||
open('https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js'),
|
open('https://unpkg.com/babel-standalone@6.15.0/babel.min.js'),
|
||||||
'js/babel-browser.min.js'
|
'js/babel.min.js'
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
|
var name = Math.random() > 0.5 ? 'Jane' : 'John';
|
||||||
var HELLO_COMPONENT = `
|
var HELLO_COMPONENT = `
|
||||||
var HelloMessage = React.createClass({
|
class HelloMessage extends React.Component {
|
||||||
render: function() {
|
render() {
|
||||||
return <div>Hello {this.props.name}</div>;
|
return <div>Hello {this.props.name}</div>;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
ReactDOM.render(<HelloMessage name="John" />, mountNode);
|
ReactDOM.render(<HelloMessage name="${name}" />, mountNode);
|
||||||
`;
|
`.trim();
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<ReactPlayground codeText={HELLO_COMPONENT} />,
|
<ReactPlayground codeText={HELLO_COMPONENT} />,
|
||||||
|
|
|
@ -1,16 +1,21 @@
|
||||||
var MARKDOWN_COMPONENT = `
|
var MARKDOWN_COMPONENT = `
|
||||||
var MarkdownEditor = React.createClass({
|
class MarkdownEditor extends React.Component {
|
||||||
getInitialState: function() {
|
constructor(props) {
|
||||||
return {value: 'Type some *markdown* here!'};
|
super(props);
|
||||||
},
|
this.handleChange = this.handleChange.bind(this);
|
||||||
handleChange: function() {
|
this.state = {value: 'Type some *markdown* here!'};
|
||||||
|
}
|
||||||
|
|
||||||
|
handleChange() {
|
||||||
this.setState({value: this.refs.textarea.value});
|
this.setState({value: this.refs.textarea.value});
|
||||||
},
|
}
|
||||||
rawMarkup: function() {
|
|
||||||
|
getRawMarkup() {
|
||||||
var md = new Remarkable();
|
var md = new Remarkable();
|
||||||
return { __html: md.render(this.state.value) };
|
return { __html: md.render(this.state.value) };
|
||||||
},
|
}
|
||||||
render: function() {
|
|
||||||
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="MarkdownEditor">
|
<div className="MarkdownEditor">
|
||||||
<h3>Input</h3>
|
<h3>Input</h3>
|
||||||
|
@ -21,15 +26,15 @@ var MarkdownEditor = React.createClass({
|
||||||
<h3>Output</h3>
|
<h3>Output</h3>
|
||||||
<div
|
<div
|
||||||
className="content"
|
className="content"
|
||||||
dangerouslySetInnerHTML={this.rawMarkup()}
|
dangerouslySetInnerHTML={this.getRawMarkup()}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
ReactDOM.render(<MarkdownEditor />, mountNode);
|
ReactDOM.render(<MarkdownEditor />, mountNode);
|
||||||
`;
|
`.trim();
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<ReactPlayground codeText={MARKDOWN_COMPONENT} />,
|
<ReactPlayground codeText={MARKDOWN_COMPONENT} />,
|
||||||
|
|
|
@ -1,26 +1,33 @@
|
||||||
var TIMER_COMPONENT = `
|
var TIMER_COMPONENT = `
|
||||||
var Timer = React.createClass({
|
class Timer extends React.Component {
|
||||||
getInitialState: function() {
|
constructor(props) {
|
||||||
return {secondsElapsed: 0};
|
super(props);
|
||||||
},
|
this.state = {secondsElapsed: 0};
|
||||||
tick: function() {
|
}
|
||||||
this.setState({secondsElapsed: this.state.secondsElapsed + 1});
|
|
||||||
},
|
tick() {
|
||||||
componentDidMount: function() {
|
this.setState((prevState) => ({
|
||||||
this.interval = setInterval(this.tick, 1000);
|
secondsElapsed: prevState.secondsElapsed + 1
|
||||||
},
|
}));
|
||||||
componentWillUnmount: function() {
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.interval = setInterval(() => this.tick(), 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
clearInterval(this.interval);
|
clearInterval(this.interval);
|
||||||
},
|
}
|
||||||
render: function() {
|
|
||||||
|
render() {
|
||||||
return (
|
return (
|
||||||
<div>Seconds Elapsed: {this.state.secondsElapsed}</div>
|
<div>Seconds Elapsed: {this.state.secondsElapsed}</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
ReactDOM.render(<Timer />, mountNode);
|
ReactDOM.render(<Timer />, mountNode);
|
||||||
`;
|
`.trim();
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<ReactPlayground codeText={TIMER_COMPONENT} />,
|
<ReactPlayground codeText={TIMER_COMPONENT} />,
|
||||||
|
|
|
@ -1,41 +1,56 @@
|
||||||
var TODO_COMPONENT = `
|
var TODO_COMPONENT = `
|
||||||
var TodoList = React.createClass({
|
class TodoApp extends React.Component {
|
||||||
render: function() {
|
constructor(props) {
|
||||||
var createItem = function(item) {
|
super(props);
|
||||||
return <li key={item.id}>{item.text}</li>;
|
this.handleChange = this.handleChange.bind(this);
|
||||||
};
|
this.handleSubmit = this.handleSubmit.bind(this);
|
||||||
return <ul>{this.props.items.map(createItem)}</ul>;
|
this.state = {items: [], text: ''};
|
||||||
}
|
}
|
||||||
});
|
|
||||||
var TodoApp = React.createClass({
|
render() {
|
||||||
getInitialState: function() {
|
|
||||||
return {items: [], text: ''};
|
|
||||||
},
|
|
||||||
onChange: function(e) {
|
|
||||||
this.setState({text: e.target.value});
|
|
||||||
},
|
|
||||||
handleSubmit: function(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
var nextItems = this.state.items.concat([{text: this.state.text, id: Date.now()}]);
|
|
||||||
var nextText = '';
|
|
||||||
this.setState({items: nextItems, text: nextText});
|
|
||||||
},
|
|
||||||
render: function() {
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h3>TODO</h3>
|
<h3>TODO</h3>
|
||||||
<TodoList items={this.state.items} />
|
<TodoList items={this.state.items} />
|
||||||
<form onSubmit={this.handleSubmit}>
|
<form onSubmit={this.handleSubmit}>
|
||||||
<input onChange={this.onChange} value={this.state.text} />
|
<input onChange={this.handleChange} value={this.state.text} />
|
||||||
<button>{'Add #' + (this.state.items.length + 1)}</button>
|
<button>{'Add #' + (this.state.items.length + 1)}</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
handleChange(e) {
|
||||||
|
this.setState({text: e.target.value});
|
||||||
|
}
|
||||||
|
|
||||||
|
handleSubmit(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
var newItem = {
|
||||||
|
text: this.state.text,
|
||||||
|
id: Date.now()
|
||||||
|
};
|
||||||
|
this.setState((prevState) => ({
|
||||||
|
items: prevState.items.concat(newItem),
|
||||||
|
text: ''
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TodoList extends React.Component {
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<ul>
|
||||||
|
{this.props.items.map(item => (
|
||||||
|
<li key={item.id}>{item.text}</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ReactDOM.render(<TodoApp />, mountNode);
|
ReactDOM.render(<TodoApp />, mountNode);
|
||||||
`;
|
`.trim();
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<ReactPlayground codeText={TODO_COMPONENT} />,
|
<ReactPlayground codeText={TODO_COMPONENT} />,
|
||||||
|
|
|
@ -90,8 +90,14 @@ var ReactPlayground = React.createClass({
|
||||||
|
|
||||||
getDefaultProps: function() {
|
getDefaultProps: function() {
|
||||||
return {
|
return {
|
||||||
transformer: function(code) {
|
transformer: function(code, options) {
|
||||||
return babel.transform(code).code;
|
var presets = ['react'];
|
||||||
|
if (!options || !options.skipES2015Transform) {
|
||||||
|
presets.push('es2015');
|
||||||
|
}
|
||||||
|
return Babel.transform(code, {
|
||||||
|
presets
|
||||||
|
}).code;
|
||||||
},
|
},
|
||||||
editorTabTitle: 'Live JSX Editor',
|
editorTabTitle: 'Live JSX Editor',
|
||||||
showCompiledJSTab: true,
|
showCompiledJSTab: true,
|
||||||
|
@ -115,15 +121,15 @@ var ReactPlayground = React.createClass({
|
||||||
this.setState({mode: mode});
|
this.setState({mode: mode});
|
||||||
},
|
},
|
||||||
|
|
||||||
compileCode: function() {
|
compileCode: function(options) {
|
||||||
return this.props.transformer(this.state.code);
|
return this.props.transformer(this.state.code, options);
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
var isJS = this.state.mode === this.MODES.JS;
|
var isJS = this.state.mode === this.MODES.JS;
|
||||||
var compiledCode = '';
|
var compiledCode = '';
|
||||||
try {
|
try {
|
||||||
compiledCode = this.compileCode();
|
compiledCode = this.compileCode({skipES2015Transform: true});
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
|
|
||||||
var JSContent =
|
var JSContent =
|
||||||
|
@ -201,13 +207,15 @@ var ReactPlayground = React.createClass({
|
||||||
} catch (e) { }
|
} catch (e) { }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var compiledCode = this.compileCode();
|
var compiledCode;
|
||||||
if (this.props.renderCode) {
|
if (this.props.renderCode) {
|
||||||
|
compiledCode = this.compileCode({skipES2015Transform: true});
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<CodeMirrorEditor codeText={compiledCode} readOnly={true} />,
|
<CodeMirrorEditor codeText={compiledCode} readOnly={true} />,
|
||||||
mountNode
|
mountNode
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
compiledCode = this.compileCode({skipES2015Transform: false});
|
||||||
eval(compiledCode);
|
eval(compiledCode);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
<script src="/react/js/jsx.js"></script>
|
<script src="/react/js/jsx.js"></script>
|
||||||
<script src="/react/js/react.js"></script>
|
<script src="/react/js/react.js"></script>
|
||||||
<script src="/react/js/react-dom.js"></script>
|
<script src="/react/js/react-dom.js"></script>
|
||||||
<script src="/react/js/babel-browser.min.js"></script>
|
<script src="/react/js/babel.min.js"></script>
|
||||||
<script src="/react/js/live_editor.js"></script>
|
<script src="/react/js/live_editor.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
Loading…
Reference in New Issue