【发布时间】:2017-06-03 19:44:54
【问题描述】:
我对 React 很陌生,在学习了一些教程之后,我尝试了下面的代码。
我制作了一个组件,在 componentWillMount 上将道具从商店传递给它,我为组件创建了一个新状态。渲染到现在都很好。
接下来,我将state 绑定到输入框的值,并且我还有onChange 侦听器。不过,我无法更改我在该字段中的值。
由于我来自 Angular 背景,我假设将输入值绑定到如下状态将自动更新 state 对象中的属性 name。我错了吗?
componentWillMount(){
this.setState({
updatable : false,
name : this.props.name,
status : this.props.status
});
}
//relevant DOM from component's render function
<input className="form-control" type="text" value={this.state.name} id={'todoName' + this.props.id} onChange={this.onTodoChange.bind(this)}/>
onTodoChange(){
console.log(this);
//consoling 'this' here, shows old values only.
//not sure how and even if I need to update state here.
// Do I need to pass new state to this function from DOM
//TODO: send new data to store
}
我的onTodoChange 函数控制台this 的值与初始化时的状态值相同。如何通过在输入框中输入来更改状态,以便将它们发送到商店?
【问题讨论】:
标签: javascript reactjs state onchange