【发布时间】:2017-09-14 20:11:24
【问题描述】:
所以,我有一个带有复选框的项目列表,就像经典的待办事项应用程序一样。
当我选择“Johnny”然后单击“删除选定的记录”时,Johnny 确实消失了。耶!
但是,一旦 Johnny 走了,Renee 就被检查了。
我没有点击 Renee。我只点击了 Johnny,但是一旦 Johnny 被删除,Renee 就会被选中。什么鬼?
显然,在复选框 DOM 保持选中状态的情况下发生了一些事情,即使我只是在其位置上清除了复选框。
我尝试通过更新 componentWillUnmount 中的状态来解决这个问题,但没有奏效。
import React, {Component} from 'react';
class Person extends Component {
constructor(props) {
super(props);
this.state = {
checked: false
}
this.checkboxToggle = this.checkboxToggle.bind(this);
}
checkboxToggle() {
this.setState({ checked: !this.state.checked }, () => {
if (this.state.checked === false) {
console.log("Checked should be FALSE");
}
else if (this.state.checked === true) {
console.log("Checked should be TRUE");
this.props.setForDelete(this.props.person._id);
}
});
}
componentWillUnmount() {
this.setState({ checked: false });
}
render() {
return (
<div>
<input type="checkbox" name="person" checked={this.state.checked} onChange={this.checkboxToggle} />
{this.props.person.name} ({this.props.person.age})
</div>
);
}
}
export default Person;
Person 组件中的其他所有内容都运行良好。删除任何记录后,如何取消选中所有复选框?
更新:这里是 PeopleList,包含所有人员的组件。
import React, { Component } from 'react';
import Person from './Person';
class PeopleList extends Component {
constructor(props) {
super(props);
}
componentWillUnmount() {
this.props.resetSetForDeleteArr();
}
render() {
return(
<div>
{this.props.people.map((person, i) => {
return <Person key={i} person={person} setForDelete={this.props.setForDelete} />;
})}
<div onClick={() => this.props.deleteRecords()}>Delete Selected Records</div>
<div onClick={() => this.props.resetSetForDeleteArr()}>Empty Array.</div>
</div>
);
}
} // end class
export default PeopleList;
【问题讨论】:
-
能展示一下渲染personlist的组件代码吗?
-
@Chris 刚刚更新。
标签: javascript reactjs dom checkbox redux