【发布时间】:2020-08-12 17:35:18
【问题描述】:
我只是想使用多个复选框的 handleChange 方法更改我的应用程序的状态。
当我单击每个复选框时,我的状态从 false 更改为 true,但是当我取消选中它时,我的状态并没有改变以反映它,我似乎无法弄清楚为什么!
import React, { Component } from "react";
export class StepOne extends Component {
constructor(props) {
super(props);
this.state = {
box1: false,
box2: false,
box3: false,
};
}
handleChange = (evt) => {
const box = evt.target.name;
this.setState({ [box]: !this.state.box });
};
render() {
return (
<div>
<input type="checkbox" name="box1" onChange={this.handleChange} />
<input type="checkbox" name="box2" onChange={this.handleChange} />
<input type="checkbox" name="box3" onChange={this.handleChange} />
</div>
);
}
}
【问题讨论】:
标签: reactjs checkbox components state