【问题标题】:Having Trouble Removing Item From Array in React在 React 中无法从数组中删除项目
【发布时间】:2020-06-26 21:00:47
【问题描述】:

加载页面后,从我的 API 中检索数据并存储在一个数组中。然后它为数组中的每个对象显示一个按钮,并且标题是适当的。

接下来需要发生的是,当我单击一个按钮时,该按钮会消失,并且关联的项目会从数组中删除。那部分不起作用。该按钮正在注册点击,并且该功能正在运行。但是按钮并没有消失,当我再次记录数组时,数组似乎没有改变。我不知道为什么。谁能帮我找出问题所在?

这是我的代码,有问题的部分是“handleAddPolicy”函数:

import React, { Component, Fragment } from 'react';
import PolicyButton from './PolicyButton/PolicyButton';

class Handbook extends Component {

    constructor(props){
        super(props);

        this.state = {
            clients: [],
            policies: [],
            client: 'Choose Client',
            logo: '',
            color1: '#000',
            color2: '#fff'
        };
        
    }

    componentDidMount() {
        fetch('/api/themes/all')
            .then(res => res.json())
            .then((result) => {
                this.setState({ clients: result });
                console.log(result);
            })
            .then(
                fetch(`/api/policy/all`)
                    .then(res => res.json())
                    .then((result) => {
                        this.setState({ policies: result });
                        console.log(result);
                    })
            );
    }

    handleAddPolicy = policyId => {
        console.log(`button clicked`);
        const policies = this.state.policies.filter(policy => policy.id !== policyId);
        this.setState({ policies: policies});
        console.log(this.state.policies);
    }

    render(){
        return(
            <Fragment>
                {/* <ClientPicker /> */}
                <div className='buttons'>
                    {this.state.policies.map(policy => (
                        <PolicyButton key={policy.id} policy={policy.policy} onAddPolicy={this.handleAddPolicy} />
                    ))}
                </div>
            </Fragment>
        );
    }
}

export default Handbook;

这是我的按钮的代码,如果有帮助,点击后应该会消失:

import React, { Component } from 'react';
import styled from 'styled-components';

class PolicyButton extends Component {
    
    state = {
        id: this.props.id, 
        policy: this.props.policy
    }

    render(){
        return(
            <button onClick={() => this.props.onAddPolicy(this.props.id)}>{this.props.policy}</button>
        )
    }
}

export default PolicyButton;

【问题讨论】:

  • setState 是异步的,所以它只会在你的 fn 堆栈被清除(以及更多事情)后改变状态
  • 你没有传递 id 作为 prop
  • 啊,好的,现在已经解决了部分问题。按钮正在消失,并且项目正在从数组中删除。但这不是正确的项目。它们似乎不是基于我数组中的实际项目 ID 号。我实际上无法说出它是如何确定要删除哪一个的。它正在为单击的按钮记录正确的 ID。但是从数组中删除的内容几乎看起来是随机的,并且在所有按钮都消失后数组中还剩下一个。
  • 另外,setState 调用后的控制台日志状态只会记录当前状态,而不是排队的下一个状态。

标签: javascript arrays reactjs react-native state


【解决方案1】:

渲染 PolicyButton 时错过了 id 属性

            <Fragment>
                {/* <ClientPicker /> */}
                <div className='buttons'>
                    {this.state.policies.map(policy => (
                        <PolicyButton
                          key={policy.id}
                          /* This is what you missed */
                          id={policy.id}
                          policy={policy.policy}
                          onAddPolicy={this.handleAddPolicy}
                        />
                    ))}
                </div>
            </Fragment>

【讨论】:

  • 啊,好的,现在已经解决了部分问题。按钮正在消失,并且项目正在从数组中删除。但这不是正确的项目。它们似乎不是基于我数组中的实际项目 ID 号。我实际上无法确定它是如何确定删除哪一个的。
猜你喜欢
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多