【发布时间】:2016-11-26 14:38:25
【问题描述】:
很多时候我们在构造函数中发送 props,但我们从不在构造函数的任何地方使用 this.props,所以为什么需要传递它以及何时需要传递。
class App extends React.Component {
constructor(props) {
super(props); // When do we need to send props to the constructor
this.state = {
data: 'Initial data...'
}
this.updateState = this.updateState.bind(this);
};
updateState(e) {
this.setState({data: e.target.value});
}
render() {
return (
<div>
<input type = "text" value = {this.state.data}
onChange = {this.updateState} />
<h4>{this.state.data}</h4>
</div>
);
}
}
【问题讨论】:
标签: javascript reactjs ecmascript-6