I looked at the lesson on [Use ES2016 Property Initializer Syntax in ES6 Classes] on egghead.io, and I'm not quite sure it is a good practice to use it. Here's the regular Reagent component, keeping state from the above lesson:
class App extends Component {
constructor() {
super()
this.state = {
todos: [],
currentTodo: ''
}
}
this.handleInputChange = this.handleInputChange.bind(this)
handleInputChange (evt) {
this.setState({
currentTodo: evt.target.value
})
}
render() {
return (
...
<TodoForm handleInputChange={this.handleInputChange}
currentTodo={this.state.currentTodo}
<TodoList todos={this.state.todos}/>
...
);
}
}
Here's the same refactored component using ES2016 syntax:
class App extends Component {
state = {
todos: [],
currentTodo: ''
}
handleInputChange = (evt) => {
this.setState({
currentTodo: evt.target.value
})
}
render() {
return (
...
<TodoForm handleInputChange={this.handleInputChange}
currentTodo={this.state.currentTodo}
<TodoList todos={this.state.todos}/>
...
);
}
}
source
share