简体   繁体   English

如何使用复选框向数组添加/删除项目?

[英]How to add/remove items to an array using checkboxes?

I am fetching items from an api and then displaying them to user with checkboxes.我正在从 api 获取项目,然后使用复选框将它们显示给用户。 When user clicks on an item the item's value should be added to user's items array.当用户点击一个项目时,项目的值应该被添加到用户的项目数组中。 Also by unchecking it, it should be removed from the array...同样通过取消选中它,它应该从数组中删除......

I know the logic behind checkboxes and state and arrays, however I simply cannot understand what is going wrong here in my code.我知道复选框、状态和数组背后的逻辑,但是我根本无法理解我的代码中出了什么问题。 My code indeed adds items to user's array but when I try to uncheck the item all of the items except the clicked item are removed...我的代码确实将项目添加到用户的数组中,但是当我尝试取消选中该项目时,除单击的项目之外的所有项目都被删除...

class StepThree extends Component {
    constructor(props) {
        super(props);
        this.state = {
            selectedItems: []
        };
    };

    handleCheckbox = e => {
        let { selectedItems } = this.state;
        const item = e.target.name;
        if (selectedItems.includes(item)) {
            const index = selectedItems.indexOf(item);
            this.setState({ selectedItems: selectedItems.splice(index, 1)});
        } else {
            selectedItems = selectedItems.concat(item);
            this.setState({ selectedItems: selectedItems });
        }
    };

    render() {
        const { categories } = this.props;
        const { selectedItems } = this.state;

        return (
            <Content>
                {categories.map(item => (
                    <label key={item.name}>
                        {item.name}
                        <Checkbox
                            name={item.name}
                            checked={selectedItems.includes(item.name)}
                            onChange={this.handleCheckbox}
                            />
                    </label>
                ))}
            </Content>
        );
    }
}

note: sorry for a very bad example of the code, I am still kind of new in react.注意:对于代码的一个非常糟糕的例子,我很抱歉,我对反应还是有点陌生​​。 However if you have any other tips, please advise.但是,如果您有任何其他提示,请提出建议。

.splice() will return the removed items from the array. .splice() 将返回从数组中删除的项目。

It will work if you call it, and then set selectedItems state:如果您调用它,然后设置 selectedItems 状态,它将起作用:

if (selectedItems.includes(item)) { 
  const index = selectedItems.indexOf(item); 
  selectedItems.splice(index, 1); 
  this.setState({ selectedItems }); 
}

array.splice() removes the selected item from the underlying array and returns an array of the deleted items. array.splice()从底层数组中删除所选项目并返回已删除项目的数组。 To fix this you can just change this.setState({ selectedItems: selectedItems.splice(index, 1)});要解决此问题,您只需更改this.setState({ selectedItems: selectedItems.splice(index, 1)}); to this.setState({ selectedItems: selectedItems.filter(selectedItem => selectedItem === item)});this.setState({ selectedItems: selectedItems.filter(selectedItem => selectedItem === item)});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM