Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RTK query based on data from another request

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading