RTK查询总是返回缓存的数据。无效

发布于 2025-01-27 20:51:16 字数 2028 浏览 2 评论 0原文

有人可以告诉我为什么在我无效的Getuser查询后仍然获得缓存的数据?

api.ts:

export const api = createApi({
  reducerPath: 'api',
  baseQuery: fetchBaseQuery({
    baseUrl: REACT_APP_API_URL,
    prepareHeaders: (headers, { getState }) => {
      headers.set('Accept', 'application/json');
      const token = (getState() as RootState).auth.token;
      if (token) {
        headers.set('Authorization', token);
      }
      return headers;
    },
  }),
  tagTypes: [
    'UserGet',
    'UserPost',
  ],
  endpoints: () => ({}),
});

usergetapi.ts:

const userGetApi = api.injectEndpoints({
  endpoints: (builder) => ({
    getUserData: builder.query<UserData, void>({
      query: () => '/users/me',
      providesTags: ['UserGet'],
    }),
  }),
  overrideExisting: true,
});
export const { useGetUserDataQuery } = userGetApi;

userpostapi.ts:

const userPostApi = api.injectEndpoints({
  endpoints: (builder) => ({
    saveUser: builder.mutation<void, OnboardingEntry>({
      query: (userEntries) => {
        const formData = Object.keys(userEntries).reduce((formData, key) => {
          formData.append(key, userEntries[key].toString());
          return formData;
        }, new FormData());
        return {
          url: '/users/update',
          method: 'POST',
          body: formData,
        };
      },
      invalidatesTags: ['UserGet'],
    }),
  }),
  overrideExisting: true,
});

export const { useSaveUserMutation } = userPostApi;

我称之为2个钩子:

const { data: { data } = {}, isLoading, isError, isSuccess } = useGetUserDataQuery();
const [saveUser, { isLoading: postIsLoading, isSuccess: postIsSuccess }] = useSaveUserMutation();

致电saveuser( ...),我被重定向到另一页。当我重新访问页面时,我希望从UseGetuserDataquery()看到更新的用户数据,但是我看到了先前的数据。即使我关闭并重新打开了应用程序,我仍然会获取旧数据!

那我在这里做错了什么?我正在使用“ Providestags”&amp;如文档中所述,“无效”。

Can somebody tell me why I still get the cached data after I invalidate the getUser query?

api.ts:

export const api = createApi({
  reducerPath: 'api',
  baseQuery: fetchBaseQuery({
    baseUrl: REACT_APP_API_URL,
    prepareHeaders: (headers, { getState }) => {
      headers.set('Accept', 'application/json');
      const token = (getState() as RootState).auth.token;
      if (token) {
        headers.set('Authorization', token);
      }
      return headers;
    },
  }),
  tagTypes: [
    'UserGet',
    'UserPost',
  ],
  endpoints: () => ({}),
});

userGetApi.ts:

const userGetApi = api.injectEndpoints({
  endpoints: (builder) => ({
    getUserData: builder.query<UserData, void>({
      query: () => '/users/me',
      providesTags: ['UserGet'],
    }),
  }),
  overrideExisting: true,
});
export const { useGetUserDataQuery } = userGetApi;

userPostApi.ts:

const userPostApi = api.injectEndpoints({
  endpoints: (builder) => ({
    saveUser: builder.mutation<void, OnboardingEntry>({
      query: (userEntries) => {
        const formData = Object.keys(userEntries).reduce((formData, key) => {
          formData.append(key, userEntries[key].toString());
          return formData;
        }, new FormData());
        return {
          url: '/users/update',
          method: 'POST',
          body: formData,
        };
      },
      invalidatesTags: ['UserGet'],
    }),
  }),
  overrideExisting: true,
});

export const { useSaveUserMutation } = userPostApi;

The 2 hooks I call:

const { data: { data } = {}, isLoading, isError, isSuccess } = useGetUserDataQuery();
const [saveUser, { isLoading: postIsLoading, isSuccess: postIsSuccess }] = useSaveUserMutation();

After calling saveUser(...), I get redirected to another page. When I revisit the page, I expect to see the updated user data from useGetUserDataQuery(), but I see the previous data. Even when I close and reopen the app, I still get the old data!

So what am I doing wrong here? I'm using 'ProvidesTags' & 'InvalidatesTags' as stated in the docs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞笑我太脆弱 2025-02-03 20:51:16

因此,经过几天的挫败感,我找到了解决方案: API标头

baseQuery: fetchBaseQuery({
    baseUrl: REACT_APP_API_URL,
    prepareHeaders: (headers, { getState }) => {
      headers.set('Accept', 'application/json');
      headers.set('Cache-Control', 'no-cache');
      headers.set('Pragma', 'no-cache');
      headers.set('Expires', '0');

      const token = (getState() as RootState).auth.token;
      if (token) {
        headers.set('Authorization', token);
      }
      return headers;
    },
  }),

希望这个答案也会帮助他人

So after days of pure frustration I found the solution: Api headers.

baseQuery: fetchBaseQuery({
    baseUrl: REACT_APP_API_URL,
    prepareHeaders: (headers, { getState }) => {
      headers.set('Accept', 'application/json');
      headers.set('Cache-Control', 'no-cache');
      headers.set('Pragma', 'no-cache');
      headers.set('Expires', '0');

      const token = (getState() as RootState).auth.token;
      if (token) {
        headers.set('Authorization', token);
      }
      return headers;
    },
  }),

Hopefully this answer will help others as well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文