我有有效载荷 A,它代表一个 n 深的嵌套树,它看起来像..(您可以将 section 数组视为树上的子项)
const data = {
id: 123,
sections: [{
id: 1,
sections:[{ id: 4, sections: [ { id: 5, sections: [] } ] }]
}, {
id: 2,
sections:[]
}, {
id: 3,
sections:[]
}]
};
有效载荷 A 的标准化状态如下所示
{
"entities": {
"sections": {
"1": { "id": 1, "sections": [ 4 ] },
"2": { "id": 2, "sections": [] },
"3": { "id": 3, "sections": [] },
"4": { "id": 4, "sections": [ 5 ] },
"5": { "id": 5, "sections": [] }
},
"menu": {
"123": { "id": 123, "sections": [ 1, 2, 3 ] }
}
},
"result": 123
}
如果用户要扩展第 5 节,我会离开并从服务器加载更多的孩子。标准化后从服务器返回的有效负载 B 如下所示
{
"entities": {
"sections": {
“6”: { "id": 6, "sections": [] },
},
"menu": {
"1234”: { "id": 1234, "sections": [ 6 ] }
}
},
"result": 1234
}
如何编写一个 reducer 来组合(合并/映射等)规范化有效负载 A 和规范化有效负载 B,以便新的 Redux 存储状态如下所示...
{
"entities": {
"sections": {
"1": { "id": 1, "sections": [ 4 ] },
"2": { "id": 2, "sections": [] },
"3": { "id": 3, "sections": [] },
"4": { "id": 4, "sections": [ 5 ] },
"5": { "id": 5, "sections": [6] },
“6”: { "id": 6, "sections": [] }
},
"menu": {
"123": { "id": 123, "sections": [ 1, 2, 3 ] }
}
},
"result": 123
}