RTK 查询获取查询缓存参数[关闭]

问题描述 投票:0回答:1

卸载然后挂载组件后,参数将设置为默认值,但每次卸载然后挂载组件时都会导致重复,并且 useGetUsersQuery 仍然有缓存的数据。我还需要以某种方式保存参数,以便与缓存的数据保持同步。此外,此参数应与 useGetUsersQuery 缓存数据过期同时过期。

const [params, setParams] = useState({page:0, pageSize: 20}); // <- here is an issue, params are set to default after new mount
const {data} = useGetUsersQuery(params); // <- data may by cached 

API端点:

getUsers: builder.query({
        providesTags: ["users"],
        query: (props) => {
          return {
            url: "/api/user/getAll",
            method: "GET",
            params: {
              name: props?.name,
              page: props?.page,
              pageSize: props?.pageSize,
            },
          };
        },
        // Only have one cache entry because the arg always maps to one string
        serializeQueryArgs: ({ endpointName }) => {
          console.log("endpointName:", endpointName);
          return endpointName;
        },
        // Always merge incoming data to the cache entry
        merge: (currentCache, newItems) => {
          console.log("Merge: ", currentCache, newItems);
          currentCache.push(...newItems);
        },
        // Refetch when the page arg changes
        forceRefetch({ currentArg, previousArg }) {
          console.log("Args: ", currentArg, previousArg);
          return currentArg !== previousArg;
        },
      }),

我希望在再次挂载一个组件后,它应该获取下一页或保留在同一页面上,并且不要第一次获取。

需要执行无限滚动,这与缓存数据一起工作。

reactjs rtk-query
1个回答
0
投票

尝试删除forceRefetch,然后使用标签仅在保存会影响查询结果的信息时重新获取。

© www.soinside.com 2019 - 2024. All rights reserved.