-3

I have an array ob objects that creates Close buttons depending on how many there are of them. When i click Close button i would expect the array to be updated (removed) then the button element will disappear from screen.

I have worked out in console.log but unable to properly do setState :(

Example code here: https://codesandbox.io/s/sharp-kilby-hn2d9

Any help would be greatly appreciated, i believe it's just the formatting of setState i need to do it properly, but because its array of an objects and so on i just cant figure that one out.

Marius
  • 1,664
  • 2
  • 16
  • 28

1 Answers1

-1

Here is your handleclick function rewritten, you forgot to return the state at the end after modifying it according to your needs :

  handleClick = i => {
    this.setState(state => {
      var newState = {allProjects: []};
      for (let z = 0; z < state.allProjects.length; z++) {
        const element = state.allProjects[z].employees;
        const items = element.filter(item => item !== i);
        console.log('filter', items);
        console.log('===========');
        newState.allProjects[z] = Object.assign({}, state.allProjects[z]);
        newState.allProjects[z].employees = items;
      }
      return newState;
    });
  };
dader
  • 1,304
  • 1
  • 12
  • 31