Home > other >  RTK query based on data from another request
RTK query based on data from another request

Time:07-25

I have some rtk query, that query data based on response of another request ( with axios )

const { dat } = useGetDataQuery({
  keys // it comes from another request
})

export const someApi = createApi({
  reducerPath: 'someApi',
  baseQuery,
  endpoints: (builder) => ({
    getData: builder.query<
      any,
      { keys: string[] }
    >({
      query: (arg) => {
        const { keys } = arg
        return {
          url: '/some_endpoint',
          params: {
            keys,
          },
        }
      },
    }),
  }),
})

And in this case data from previous request it's keys. Is it possible make request inside query and use this data for my query, instead of doing this request outside and pass data as params?

CodePudding user response:

I wouldn't recommend it. Theoretically it is possible, using queryFn, as you have access to getState there - but if that state value would ever change, that wouldn't re-execute your query. It's really best to just put in dynamic values as argument.

  • Related