-2

I have a react app with a state element like this:

state = {

  options: {

    xaxis: {
      categories: []
    }
  }

}

However, in this function, when I try to set the state of categories I get an error that it isn't expecting a format with periods like options.xaxis.categories

this.setState({
  options.xaxis.categories: this.state.resultDates
});

How can I properly set the state of that array element?

Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • 1
    Does this answer your question? [How to update nested state properties in React](https://stackoverflow.com/questions/43040721/how-to-update-nested-state-properties-in-react) – Amir-Mousavi May 16 '20 at 14:57

1 Answers1

1

That's not the correct syntax. This is correct syntax.

this.setState({
  options: {
    ...this.state.options,
    xaxis: {
      ...this.state.options.xaxis,
      categories: this.state.resultDates
    }
   }
});
Amit Chauhan
  • 6,151
  • 2
  • 24
  • 37
  • 1
    wow, I was way off! I don't know why I couldn't find that documented anywhere. I really appreciate it, that worked great! – Geoff_S May 16 '20 at 15:14