javascript novice
javascript novice

Reputation: 777

setState with nested JSON

I am new to RN and am trying to figure out how to use setState with nested JSON.

My object is in the following format:

{
  "data": {
    "count": 1,
    "current": [
      {
        "Id":"284"
      },
      {
        "Id":"285"
      },
      {
        "Id":"286"
      }
    ]
  }
}

I have set the state as follow:

state = { notices: [data] };

I need to update the count value and set state:

this.state.notices[0].data.count = newcount

I am trying to do it wth this.setState but I can't figure out how to set it for nested JSON.

Upvotes: 0

Views: 1066

Answers (1)

aleksandr
aleksandr

Reputation: 34

You can do this as following:

updateNotices = (newcount) => {
  const { notices } = this.state;
  const newNotices = [... notices];
  newNotices[0].data.count = newcount;
  this.setState({ notices: newNotices });
}

Then, you can call updateNotices(1).

Upvotes: 1

Related Questions