I read on the React tutorials page that ES6 will use constructor functions to initialize state like this.
export class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {count: props.initialCount};
}
tick() {
this.setState({count: this.state.count + 1});
}
render() {
return (
<div onClick={this.tick.bind(this)}>
Clicks: {this.state.count}
</div>
);
}
}
Counter.propTypes = { initialCount: React.PropTypes.number };
Counter.defaultProps = { initialCount: 0 };
Then it continues, using ES7 syntax to achieve the same thing.
// Future Version
export class Counter extends React.Component {
static propTypes = { initialCount: React.PropTypes.number };
static defaultProps = { initialCount: 0 };
state = { count: this.props.initialCount };
tick() {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<div onClick={this.tick.bind(this)}>
Clicks: {this.state.count}
</div>
);
}
}
Why is ES7 better then ES6 version or ES5 version.
Thanks
question from:
https://stackoverflow.com/questions/35662932/react-constructor-es6-vs-es7 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…