Redux RTK乐观更新导致Immer错误



我已经关注了关于RTK乐观更新的文档(和其他一些帖子(,但我似乎无法让我的案例发挥作用。这是我的createReply突变:

createReply: builder.mutation<IPost, any>({
query({...body}) {
return {
url: PostPath,
method: 'POST',
body: body,
};
},
async onQueryStarted({...body}, {dispatch, queryFulfilled}) {
const patchResult = dispatch(
resourcesApi.util.updateQueryData('getRepliesById', body.parent, (draft) => {
console.log(draft);
Object.assign(draft, body)
})
)
try {
await queryFulfilled
} catch {
console.log("query was fulfilled");
console.log(patchResult);
// patchResult.undo()
}
}
}),

这是它引用的getRepliesById查询:

getRepliesById: builder.query<IPost, string>({
query: (id: string) => `${PostPath}/${id}/replies`,
}),

当我尝试使用这种突变时,POST运行良好,但我收到了以下错误:CCD_ 2。(错误调用堆栈以onQueryStarted结束(。

我要去哪里违反Immer规则?

编辑;这是body:

const postObj = {
content: reply,
author: user._id,
parent: _parentId,
visibility: {scope: 'public'},
media: [],
ats: [],
kind: 'comment',
};
createReply(postObj);

看起来data是一个数组,现在您可以像处理object一样处理它。数组只能具有数字索引属性,而您的Object.assign似乎添加了其他属性?

最新更新