1 year ago

#376105

test-img

milas86534

RTK query - How to send parameter with selectFromResult in query

The below line is used to GET data from the api, if there is is not any result it will return [] until it has some elements.

const { data, isLoading } =  useGetDataByRangeQuery(undefined, {
      selectFromResult: result => ({...result,data: {resp:(result.data === undefined ? [] : result.data),res:result} })
    })

Now, i need to pass some data to the useGetDataByRangeQuery as it is converted from get to post method.

let post_data = {"start":"yyyy-mm-dd","name":"some_name"}

const { data, isLoading } =  useGetDataByRangeQuery(post_data,undefined, {
      selectFromResult: result => ({...result,data: {resp:(result.data === undefined ? [] : result.data),res:result} })
    })

//In query page

export const dashboardData = createApi({
  reducerPath: 'petData',
  baseQuery: fetchBaseQuery({
    baseUrl: `${PetStoreApiEndpoint}/`,
    prepareHeaders: (headers) => {
        headers.set('Content-Type', 'application/json')
        headers.set('Accept', 'application/json')
        return headers
      },
  }),
  endpoints: (builder) => ({
    getDataByRange: builder.query({ 
      query: (arg) => ({url:'pet_data', method:'POST', body: arg}),
      transformResponse: (response) => {     
        return response.Items;
      }})
  }),

reactjs

react-redux

redux-toolkit

rtk-query

0 Answers

Your Answer

Accepted video resources