简体   繁体   中英

How can I update a state inside an array of object with Redux?

My app has 2 tabs and I need to modify the content according the click in tab I have this state:

const example = {
  tabs: [
    {
      id: 111,
      title: 'Python',
      icon: 'Python@2x.svg',
      content: 'Hello World'
    },
    {
      id: 333,
      title: 'EQL',
      icon: 'EQL@2x.svg',
      content: 'Hello World 2'
    }
  ]
}

and I need to change the 'content' inside of tabs. I tried this:

return {
    ...state,
    tabs: {
      ...state.tabs,
      content: 'Uhuuuul'
    }
}

But this didn't work! Could someone explain why?

Use map :

return {
    ...state,
    tabs: state.tabs.map(tab => ({...tab, content: "Uhuuuul"}))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM