【发布时间】:2018-02-17 21:49:51
【问题描述】:
如何获取文本字段值并将该值推送到 react js 中的 arrayList?
我想从文本框中获取值并将其推送到 Modules 数组,以便我可以通过迭代来呈现值。
我尝试使用 ref 但出现错误。
你能帮帮我吗?
constructor(props) {
super(props);
this.state={
module:'',
Modules: []
}
}
change (event){
this.setState({
[event.target.name]:event.target.value
});
};
createModule (e) {
e.preventDefault();
console.log("submitted",this.state.module);
this.setState(previousState => ({
...state,
thisModules: [...previousState.Modules, 'new value']
}));
};
render(){
return(
<form className="form-inline">
<div className="form-group">
Module Name:
<input type="text" id="module"
name="module"
placeholder="module"
className="form-control"
ref="Module"
value ={this.state.module}
onChange={event => this.change(event)}/>
<button type="submit" className="btn btn-primary" onClick={(event) => this.createModule(event)}>Add Module</button>
</div>
</form>
【问题讨论】:
-
到目前为止你的代码是什么样的?可以发一下吗?
-
我想获取文本字段值并将值推送到 ArrayList,以便我可以呈现这些值。
标签: reactjs react-redux