简体   繁体   English

从阵列中删除了错误的项目

[英]Wrong item deleted from array

I have a component that contains an array of items in its state, but whenever I try to delete an item, the wrong one is deleted. 我有一个组件,该组件在其状态下包含一个项目数组,但是每当我尝试删除一个项目时,都会删除错误的项目。

Here's a simplified version of my parent Component: 这是我的父组件的简化版本:

export default class BankList extends Component {
    state = {
        banks: [new Bank("Name1"), new Bank("Name2")]
    }

    addBank() {
        this.setState({banks: [...this.state.banks, new Bank("")]})
    }

    removeBank(index) {
        let good = [];
        this.state.banks.forEach((item, ind) => {
            if(ind !== index){
                good.push(item);
            }
        });
        this.setState({banks: good}); 
    }

    render() {
        return (
            <Container>
                <Content>
                    <List>
                        {this.state.banks.map((bank, index) => <BankContainer bank={bank} key={index} id={index} onRemove={() => this.removeBank(index)}/>)}
                    </List>
                    <Content>
                        <Right>
                            <Button onPress={() => this.addBank()}>
                                <Text>Add Bank</Text>
                            </Button>
                        </Right>
                    </Content>
                </Content>
            </Container>
        )
    }
}

The BankContainer class simply shows the Bank, and when the "remove" button inside it is pressed, it will call onChange with the provided id. BankContainer类仅显示银行,当按下其中的“删除”按钮时,它将使用提供的ID调用onChange。 I have verified that the right index is being passed into removeBank. 我已验证正确的索引已传递到removeBank中。 But, after removeBank executes, the last item (index 1) is removed from the banks array, when I selected the first one (index 0). 但是,在执行removeBank之后,当我选择第一个项目(索引0)时,最后一个项目(索引1)将从banks数组中删除。 I have tried the following method bodies in removeBank, but to no avail: 我在removeBank中尝试了以下方法主体,但无济于事:

Tried a shallow copy: 尝试了浅表副本:

let old = [...this.state.banks];
old.filter((item, ind) => ind !== index);
this.setState({banks: old});

Tried a straight filter: 尝试了一个直过滤器:

this.setState({banks: this.state.banks.filter((item, ind) => ind !== index);

Tried a deep copy: 尝试了深拷贝:

let old = [...this.state.banks];
old = old.map(i => Object.assign({}, i));
old.filter((item, ind) => ind !== index);
this.setState({banks: old});

Tried a splice: 试了一个接头:

this.setState({banks: this.state.banks.splice(index, 1)});

Tried a shallow copy and splice: 尝试了浅拷贝和拼接:

let old = [...this.state.banks]
old = old.splice(index, 1);
this.setState({banks: old});

Tried a deep copy and splice: 尝试了深层复制和拼接:

let old = [...this.state.banks];
old = old.map(i => Object.assign({}, i));
this.setState({banks: old.splice(index, 1)})

None of these have worked, they have all exhibited the same behavior. 这些都不起作用,它们都表现出相同的行为。 I'm at my wits end on this, any suggestions would be hugely appreciated! 我为此竭尽全力,任何建议将不胜感激!

According to the docs : 根据文档

We don't recommend using indexes for keys if the order of items may change. 如果项目的顺序可能更改,我们不建议对索引使用索引。 This can negatively impact performance and may cause issues with component state. 这可能会对性能产生负面影响,并可能导致组件状态出现问题。 Check out Robin Pokorny's article for an in-depth explanation on the negative impacts of using an index as a key. 查阅Robin Pokorny的文章 ,深入了解使用索引作为键的负面影响。 If you choose not to assign an explicit key to list items then React will default to using indexes as keys. 如果您选择不为列表项分配显式键,那么React将默认使用索引作为键。

Using a stable ID, and using that to remove elements from the array, should solve your issues. 使用稳定的ID,并使用该ID从数组中删除元素,应该可以解决您的问题。

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

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