-
Notifications
You must be signed in to change notification settings - Fork 47.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update the homepage with ES6 #7868
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ require('open-uri') | |
desc "download babel-browser" | ||
task :fetch_remotes do | ||
IO.copy_stream( | ||
open('https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js'), | ||
'js/babel-browser.min.js' | ||
open('https://unpkg.com/babel-[email protected]/babel.min.js'), | ||
'js/babel.min.js' | ||
) | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
var TIMER_COMPONENT = ` | ||
var Timer = React.createClass({ | ||
getInitialState: function() { | ||
return {secondsElapsed: 0}; | ||
}, | ||
tick: function() { | ||
this.setState({secondsElapsed: this.state.secondsElapsed + 1}); | ||
}, | ||
componentDidMount: function() { | ||
this.interval = setInterval(this.tick, 1000); | ||
}, | ||
componentWillUnmount: function() { | ||
class Timer extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = {secondsElapsed: 0}; | ||
} | ||
|
||
tick() { | ||
this.setState({ | ||
secondsElapsed: this.state.secondsElapsed + 1 | ||
}); | ||
} | ||
|
||
componentDidMount() { | ||
this.interval = setInterval(() => this.tick(), 1000); | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.interval); | ||
}, | ||
render: function() { | ||
} | ||
|
||
render() { | ||
return ( | ||
<div>Seconds Elapsed: {this.state.secondsElapsed}</div> | ||
); | ||
} | ||
}); | ||
} | ||
|
||
ReactDOM.render(<Timer />, mountNode); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it would be more similar to real usage if you used document.getElementById('root') or similar here, rather than mountNode? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but this would require some further changes to how this page is set up (e.g. passing IDs as props to the code editor). Can do separately. |
||
`; | ||
`.trim(); | ||
|
||
ReactDOM.render( | ||
<ReactPlayground codeText={TIMER_COMPONENT} />, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,56 @@ | ||
var TODO_COMPONENT = ` | ||
var TodoList = React.createClass({ | ||
render: function() { | ||
var createItem = function(item) { | ||
return <li key={item.id}>{item.text}</li>; | ||
}; | ||
return <ul>{this.props.items.map(createItem)}</ul>; | ||
class TodoApp extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.handleChange = this.handleChange.bind(this); | ||
this.handleSubmit = this.handleSubmit.bind(this); | ||
this.state = {items: [], text: ''}; | ||
} | ||
}); | ||
var TodoApp = React.createClass({ | ||
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() { | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<h3>TODO</h3> | ||
<TodoList items={this.state.items} /> | ||
<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> | ||
</form> | ||
</div> | ||
); | ||
} | ||
}); | ||
|
||
handleChange(e) { | ||
this.setState({text: e.target.value}); | ||
} | ||
|
||
handleSubmit(e) { | ||
e.preventDefault(); | ||
var newItem = { | ||
text: this.state.text, | ||
id: Date.now() | ||
}; | ||
this.setState({ | ||
items: [...this.state.items, newItem], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about just using concat to avoid ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also - wasn't someone talking recently about, you shouldn't use this.state.foo in setState, because there's some race condition if you do it twice per update cycle? I really prefer the look of this code, but, I just want to make sure we're not suggesting something buggy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It used to be
Ideally you shouldn't but the alternative is too much syntax noise for this example so I think it's okay. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not quite a race condition, it's that |
||
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); | ||
`; | ||
`.trim(); | ||
|
||
ReactDOM.render( | ||
<ReactPlayground codeText={TODO_COMPONENT} />, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we reach a decision on whether to use the updater form here or not? Even though it doesn't matter for a Timer where state is only updating once a second, people will see this and use it elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.