[英]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.