Search code examples
react-nativesetstatespread

React setState to update field of a nested Object


I am working on my App in React Native, and I was using to use the spread operator to keep the state of this object coming from backend, the problem is that I cannot update the field of a nested object inside this big object.

Example:

{
  foo: "bar",
  lee: {
     fee: "yoo"
  }
}

using React setState, how can I access the fee key of the lee object? I tried with setState(...object, object.lee.fee: value) but this seems not working.

Any good tip?


Solution

  • Try this way

    var someProperty = {...this.state.someProperty}
    someProperty.lee = {fee : value};
    this.setState({someProperty})