1

I have the following data structure:

        this.state = {
        client: {
            abilities: [],
        },
        abilitiesDisplay: [],

and I am trying to set state of the client abilities empty array to abilitiesDisplay array, which has been mutated in the code and now has two items.

My set state function is as follows:

EDIT: the client and abilitiesDisplay are declared earlier in the function:

    const {
        client,
        abilitiesDisplay
    } = this.state;


           this.setState(() => ({
                client: {
                    ...this.state.client,
                    abilities: [
                        ...client.abilities,
                        abilitiesDisplay
                    ],
                }
            }))

The set state is not working in this case and client.abilities is still and empty array. Is the error obvious?

6
  • reactjs.org/docs/… You need to use state.client Commented Nov 8, 2018 at 19:42
  • @SLaks: thank you, could you elaborate? use it where? Commented Nov 8, 2018 at 19:43
  • Please provide a complete example. Your second code snippet refers to variables named client and abilitiesDisplay without showing their declarations nor initializations. Commented Nov 8, 2018 at 19:49
  • @Code-Apprentice: thank you for the comment, I have updated the code block Commented Nov 8, 2018 at 19:57
  • What are the values of client.abilities and abiaitiesDisplay? Commented Nov 8, 2018 at 20:10

3 Answers 3

1

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>

Sign up to request clarification or add additional context in comments.

13 Comments

@ksenia You're welcome! Ah, I misunderstood your question. I have updated the answer.
@ksenia I'm not sure I understand. Doesn't it work like you intended if you press "Run the snippet"? It doesn't matter if one of the arrays are empty, it will still work.
@ksenia Don't use push. That will mutate the state. Sorry, are you saying my example is broken? I don't understand. Are you really spreading both arrays?
@ksenia client.abilities is a reference to the old array. Write this.client.abilities to get the new one instead.
@ksenia Great! You're welcome. Again, use this.client instead. You are never mutating data in React/Redux, so you can't use old references.
|
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.

Comments

0

You need this.state.abilitiesDisplay.

1 Comment

Can you elaborate on your answer?

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.