dro*_*dil 5 javascript redux react-redux rtk-query
我为 RTK 查询定义了以下 API:
export const postsApi = createApi({
reducerPath: "postsApi",
baseQuery: fetchBaseQuery({ baseUrl: 'https://myservice.co/api/v2/' }),
tagTypes: ["Posts"],
endpoints: (builder) => ({
getAllPosts: builder.query({
query: () => ({ method: "GET", url: "/posts" }),
transformResponse: (response) => response.posts,
providesTags: (result) =>
result
? [
...result.map(({ id }) => ({ type: "Posts", id })),
{ type: "Posts", id: "LIST" },
]
: [{ type: "Posts", id: "LIST" }],
}),
updatePost: builder.mutation({
query: ({ postId, ...body }) => ({
url: `/posts/${postId}`,
method: "POST",
config: { body },
}),
invalidatesTags: (_, __, arg) => [{ type: "Posts", id: arg.id }],
}),
getPost: builder.query({
query: (postId) => ({
method: "GET",
url: `/posts/${postId}`,
}),
providesTags: (_, __, id) => [{ type: "Posts", id }],
}),
}),
});
export const { useGetAllPostsQuery, useUpdatePostMutation, useGetPostQuery } = postsApi;
Run Code Online (Sandbox Code Playgroud)
我希望这样做的是,当成功调用 updatePost 时,它只会使单个帖子的缓存失效,并使用 getPost 查询来重新获取信息,而不是 getAllPosts。这无论如何可能吗?这些帖子显示在使用 getAllPosts 查询获取的表中。
不。RTK-Query 是文档缓存(完整响应 = 文档),而不是标准化缓存。它对缓存响应的内容及其结构一无所知——并且它永远不会尝试自己在其中“缝合”任何内容。
您可以通过乐观更新手动执行此操作,但 RTK-Q 自动执行的所有操作都是重新获取,这在大多数用例中应该足够了。
归档时间: |
|
查看次数: |
2789 次 |
最近记录: |