如何使用 injectEndpoints 进行乐观更新
how to do an optimistic update with injectEndpoints
如何使用 injectEndpoints 构建器使用 redux 工具包创建乐观更新?
我有一个 api 以这种方式构造的调用
import api from './api';
export const heartApi = api.injectEndpoints({
endpoints: (builder) => ({
createHeart: builder.mutation({
query: (id: string) => ({ url: '/hearts', method: 'POST', body: { id } }),
}),
}),
});
export const { useCreateHeartMutation } = heartApi;
docs 提到您可以使用 onQueryStarted
来基于该突变乐观地更新请求的缓存,但是我不知道在哪里可以找到使用 injectEndpoints
方法。
这里使用 createApi
或 injectEndpoints
绝对没有区别,在这两种情况下它都是 builder.mutation
调用的一部分:
export const heartApi = api.injectEndpoints({
endpoints: (builder) => ({
createHeart: builder.mutation({
query: (id: string) => ({ url: '/hearts', method: 'POST', body: { id } }),
onQueryStarted(arg, {queryFulfilled}){
// it would go here
}
}),
}),
});
如何使用 injectEndpoints 构建器使用 redux 工具包创建乐观更新? 我有一个 api 以这种方式构造的调用
import api from './api';
export const heartApi = api.injectEndpoints({
endpoints: (builder) => ({
createHeart: builder.mutation({
query: (id: string) => ({ url: '/hearts', method: 'POST', body: { id } }),
}),
}),
});
export const { useCreateHeartMutation } = heartApi;
docs 提到您可以使用 onQueryStarted
来基于该突变乐观地更新请求的缓存,但是我不知道在哪里可以找到使用 injectEndpoints
方法。
这里使用 createApi
或 injectEndpoints
绝对没有区别,在这两种情况下它都是 builder.mutation
调用的一部分:
export const heartApi = api.injectEndpoints({
endpoints: (builder) => ({
createHeart: builder.mutation({
query: (id: string) => ({ url: '/hearts', method: 'POST', body: { id } }),
onQueryStarted(arg, {queryFulfilled}){
// it would go here
}
}),
}),
});