微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RTK:如何使缓存数据无效以响应更新?

如何解决RTK:如何使缓存数据无效以响应更新?

我正在尝试使用 RTK 查询使缓存数据无效。但不知何故它对我不起作用。如果更新的配置文件 ID(在我的情况下为 cognitoID)在 ProfileList 中,我想再次获取 ProfileList。我可以保证 arg.id(代表 cognitoId)正确传递给 updateProfile 函数,并且 cognitoId 包含在 ProfileList 响应中。

如果我通过 provideTags: ["Profile"]invalidateTags:["Profile"] 选择更细粒度的选项,则失效有效,但在以下设置中失败。

function providesProfileList(resultsWithIds,tagType) {
  return resultsWithIds
    ? [
        { type: tagType,id: "LIST" },...resultsWithIds.map(({ id }) => ({
          type: tagType,id: resultsWithIds.cognitoId,})),]
    : [{ type: tagType,id: "LIST" }];
}

export const idApi = createApi({
  reducerPath: "idApi",baseQuery: fetchBaseQuery({
    baseUrl: process.env.REACT_APP_BACKEND_URL,prepareHeaders: (headers) => {
      headers.set("Authorization",`Bearer ${localStorage.jwtToken2}`);

      return headers;
    },}),tagTypes: ["Profile"],endpoints: (builder) => ({
     getProfileListViaId: builder.query({
      query: ({ page,placeID,filter,search }) => ({
        url: `users?limit=9${page ? `&offset=${page * 9 - 9}` : ""}${
          placeID ? `&place=${placeID}` : ""
        }${filter && filter.length ? `&${filter}` : ""}${
          search ? `&search=${search}` : ""
        }`,method: "GET",providesTags: (result) => providesProfileList(result.data,"Profile"),transformResponse: (response) => {
        const obj = {
          data: response.results,pageCount: Math.ceil(response.count / 9),};

        return obj;
      },async onQueryStarted(arg,{ dispatch,queryFulfilled }) {
        dispatch(showLoading());
        queryFulfilled.then(
          (res) => {
            dispatch(hideLoading());
          },(err) => {
            dispatch(hideLoading());
          }
        );
      },updateProfile: builder.mutation({
      query(data) {
        const { id,body } = data;
        return {
          url: `users/${id}/`,method: "PATCH",body,};
      },// Invalidates all queries that subscribe to this Profile `id` only.
      // `getProfileList` *might*  rerun,if this id was under its results.
      invalidatesTags(result,error,arg) {
        console.log(arg.id);
        return [{ type: "Profile",id: arg.id }];
      },});

export const {
  //Profile
  useGetProfileListViaIdQuery,useUpdateProfileMutation,} = idApi;

我很高兴得到任何提示

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。