我有一个组件,它是一个使用Redux—Toolkit Query(RTKQ)获取8个项目的 carousel ,另一个在侧边栏上的组件,它也使用RTKQ获取3个项目.它们都在同一时间呈现,并且使用相同的RTKQ,有时候,有一个bug,使它们共享相同数量的项目(虽然它们不应该)
他们都使用这个查询.
const { data } = useGetPopularClipsQuery({ limit: 3, page: 1 });
const { data } = useGetPopularClipsQuery({ limit: 8, page: 0 });
RTKQ
getPopularClips: builder.query<
{ posts: Array<Post>; hasMore: boolean },
{ limit?: number; page: number }
>({
providesTags: ['PopularClips'],
query: ({ limit = 5, page }) => ({
url: `/post/popular-clips`,
params: {
limit,
page
}
}),
transformResponse(baseQueryReturnValue: Post[], meta, arg) {
return {
posts: baseQueryReturnValue.map((clip) => ({
...clip,
...arg,
fromQuery: `getPopularClips`
})),
hasMore: baseQueryReturnValue.length > 0havr
};
},
serializeQueryArgs: ({ endpointName }) => endpointName,
merge: (currentCache, newItems) => ({
posts: uniqBy(
[...currentCache.posts, ...newItems.posts],
(clip) => clip.id
),
hasMore: newItems.posts.length > 0
}),
forceRefetch: ({ currentArg, previousArg }) =>
currentArg?.page !== previousArg?.page
}),
两个组件必须同时呈现,但它们应该具有不同数量的项目.