【发布时间】:2019-09-26 20:47:32
【问题描述】:
整个想法是让用户在表单中输入并在 JSON 对象中显示他们的输入。在状态下,我有一个数组,里面还有另一个数组。
我的 Form.js 是这样的,
state= {
groups:[{
typeA:[{}],
typeB:[{}]
}],
credentials: false
};
change = e =>{
this.setState({[e.target.name]: e.target.value})
};
handleSubmit(event) {
event.preventDefault();
this.setState({
credentials: true
});
}
render(){
return(
<div class="classform">
<form >
<label>
Field1:
<br/>
<input type="text"
name="typeA"
placeholder="Type A"
//store it as the first element of the type A
value={this.state.groups.typeA[0]}
onChange={this.change.bind(this)}
/>
//other fields with the same code
随后,Field2 将被存储为类型 A 的第二个元素 然后,Field3 和 Field4 将被存储为 B 类型数组的 2
我希望代码能给我这样的输出:
"typeA": ["field1 value", "field2 value"],
"typeB": ["field3 value", "field4 value"]}
我是 React 的初学者,我无法将字段值存储为数组状态。
【问题讨论】:
标签: javascript arrays reactjs state