简体   繁体   English

如何使用 Redux 更新对象数组中的状态?

[英]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:我的应用程序有 2 个选项卡,我需要根据选项卡中的点击修改内容,我有以下状态:

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 :使用map

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我在对象中有一个数组并且该对象在一个大数组中,我该如何更新 React Native 状态? - How can I update the React Native state if I have an array inside an object and the object is inside in a big array? 如何在 redux 中更新 object 内的数组 - How do I update array inside object in redux 我如何从 Redux/react 中的数组 state 中删除 object - How can i remove an object from an array state in Redux/react 无法在REACT / REDUX中获得状态以正确更新对象内部的数组 - Can't get state to update array inside of an object correctly in REACT/REDUX 我如何从 React-Redux 中的 reducer 中的状态更新对象数组中单个元素的名称字段? - How can i update a name field from single element in array of object from a state in reducer in React-Redux? 如何使用 Redux 中的传入对象更新状态(对象数组)? - How to update state (array of objects) with incoming object in Redux? 我如何使用 state 更新数组 object 的值 - how i can update the value of object of array by use state 在Redux Reducer中,如何更新数组对象内的1个属性 - In redux reducer how to update 1 property inside array object 如何在反应 redux 减速器 state 中更改数组中的 object 值? - How can I change an object value in an array in react redux reducer state? 如何在 ReactJS 的 Reducer 中更新 redux 状态? - How can I update redux state in Reducer in ReactJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM