0

Upon loading the page, data is retrieved from my API and stored in an array. It then displays a button for each object in the array and titles is appropriately.

What needs to happen next is when I click a button, that button disappears, and the associated item removed from the array. That part is not working. The button is registering the click, and the function is running. However the button does not disappear, and when I log the array again, the array appears to be unchanged. I cannot figure out why. Can anyone help me spot the issue?

Here is my code, the part in question is the "handleAddPolicy" function:

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;

And here is code for my button that should disappear after being clicked if it helps:

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;
4
  • setState is async so it will only change the state after your fn stack is cleared (and many more things) Commented Jun 26, 2020 at 21:03
  • 1
    You've not passed id as prop Commented Jun 26, 2020 at 21:04
  • Ah, ok, that has fixed part of the issue now. The buttons are disappearing, and items are being removed from the array. But it's not the correct items. They do not seem to be based on the actual item id numbers in my array. I can't actually tell how it's determining which one be removed. It is logging the correct id for the button clicked. But what is being removed from the array almost seems random, and there's one left over in the array after all the buttons are gone. Commented Jun 26, 2020 at 21:14
  • Also, console logging state immediately after setState call will only log current state, not the next state queued up. Commented Jun 26, 2020 at 21:36

1 Answer 1

1

You missed the id prop when rendering PolicyButton

            <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>
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, ok, that has fixed part of the issue now. The buttons are disappearing, and items are being removed from the array. But it's not the correct items. They do not seem to be based on the actual item id numbers in my array. I can't actually tell how it's determining which one be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.