【发布时间】:2023-03-31 04:00:02
【问题描述】:
我如何在 reducer 中写这个来改变状态?
doc = {
id:"zf123ada123ad",
name:"examp",
subdoc:{
name:"subdoc examp",
subsubdoc:[{
id:"zcgsdf123zaar21",
subsubsubdoc:[{
id:"af2317bh123",
value: "heyhey" //this value I want to update
}]
}]
}
}
假设我有一个看起来像这样的减速器 action.payload 看起来像这样 { theInputId1: "someId", theInputId2: "另一个Id", 输入值:“一些值” }
export function updateSubSubSubDoc(state = {}, action){
switch(action.type){
case 'UPDATE_THE_SUBSUBSUB':
return {
state.doc.subdoc.subsubdoc.find(x => x.id ==
theInputId1).subsubsubdoc.find(x => x.id == theInputId2).value = theInputValue // just example code for you to understand where i'm going.
}
default:
return state
}
}
我想要做的是更新一个处于当前状态的 subsubsub 文档
【问题讨论】:
-
不清楚你如何代表国家。最初它是
false,然后突然变成了具有doc.subdoc.subsubdoc甚至具有where方法的东西。 -
复制时我打错字了
-
我更新了,where方法没有实现。但它应该是一个在对象数组中通过 id 查找和对象的查询
-
您的意思是
find? MDN -
这是一个很好的例子,说明为什么你的状态不应该深度嵌套。
标签: javascript reactjs redux