score:1

Accepted answer

if you want to add the elements of the abilitiesdisplay array to the client.abilities array, you can create a new array that contains all the elements in both arrays by spreading them both.

example

class app extends react.component {
  state = {
    client: {
      abilities: ["baz"]
    },
    abilitiesdisplay: ["foo", "bar"]
  };

  componentdidmount() {
    settimeout(() => {
      this.setstate(previousstate => ({
        client: {
          abilities: [
            ...previousstate.client.abilities,
            ...previousstate.abilitiesdisplay
          ]
        }
      }));
    }, 2000);
  }

  render() {
    return <div>{json.stringify(this.state)}</div>;
  }
}

reactdom.render(<app />, document.getelementbyid("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="root"></div>

score:0

you need this.state.abilitiesdisplay.

score:1

try this =)

this.setstate((prevstate) => ({
  client: { 
    abilities: [ ...this.prevstate.abilitiesdisplay ]
  }
}));

i assume that you don't want to keep the old values in the abilities by your information above.


Related Query

More Query from same tag