【问题标题】:Redux Toolkit RTK Query call endpoints in queryFnRedux Toolkit RTK 在 queryFn 中查询调用端点
【发布时间】:2021-11-04 12:06:50
【问题描述】:

我使用带有queryFn 而不是query 的端点来执行许多请求。有没有办法调用已经定义的端点而不是使用 fetchWithBQ
这是一个例子。

export const api = createApi({
  reducerPath: "api",
  baseQuery: fetchBaseQuery({
    baseUrl: "url",
  }),
  endpoints: (builder) => {
    return {
      device: builder.query<Device, string>({
        query: (id) => `devices/${id}`, // repeat 1
      }),
      deployments: builder.query<Deployment[], string>({
        queryFn: async (arg, _api, _extraOptions, fetchWithBQ) => {
            // I would preferred to call the device endpoint directly.
            // It will prevent to repeat the url and get cached data.
          const result = await fetchWithBQ(`devices/${arg}`); // repeat 2
          return ...
        },
      }),
    };
  },
});

【问题讨论】:

    标签: reactjs redux react-redux redux-toolkit rtk-query


    【解决方案1】:

    不,目前这是不可能的,因为它会在整个事情中添加“什么取决于其他内容”的跟踪,并且内部管理会变得非常复杂。

    您通常只需使用两个useQuery 挂钩即可进行相关查询。当然,对于抽象,您可以将它们组合成一个自定义钩子。

    const useMyCustomCombinedQuery = (arg) => {
      const result1 = useMyFirstQuery(arg)
      const result2 = useMySecondQuery(result1.isSuccess ? result1.data.something : skipToken)
    
      return {result1, result2}
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 2021-11-06
      • 2021-12-28
      • 2023-01-15
      • 2022-07-12
      • 2022-10-08
      • 2022-12-17
      • 2022-10-07
      • 1970-01-01
      相关资源
      最近更新 更多