我正在尝试创建具有关系ID的实体。检查了关系数据的normalizr示例,但是在分页时,旧的post id数组被替换为post id的新数组,它们没有被合并。
初始实体:
const entities = {
users: {
testUser: {
posts: ['1', '2']
}
}
};
分页后会发生什么:
const entities = {
users: {
testUser: {
posts: ['3', '4']
}
}
};
分页后我的期望:
const entities = {
users: {
testUser: {
posts: ['1', '2', '3' ,'4']
}
}
};
这是代码,我错过了什么?谢谢!
const processStrategy = (value, parent, key) => {
switch (key) {
case 'author':
return { ...value, posts: [parent.id] };
default:
return { ...value };
}
};
const mergeStrategy = (entityA, entityB) => {
return {
...entityA,
...entityB,
posts: [...(entityA.posts || []), ...(entityB.posts || [])],
};
};
const user = new schema.Entity(
'users',
{},
{
idAttribute: 'username',
mergeStrategy,
processStrategy
}
);