Search code examples
reactjsreduxrtk-query

RTK Query query parameter


I want to send request like this https://pixabay.com/api/?key=1231231231231&per_page=10. But this code sends request like this https://pixabay.com/api/?key=1231231231231/per_page=10. How do I fix it? Can't get how to set query parameter

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const api = createApi({
  reducerPath: "api",
  baseQuery: fetchBaseQuery({
    baseUrl: `https://pixabay.com/api/?key=${process.env.REACT_APP_API_KEY}`,
  }),
  endpoints: builder => ({
    getPhotos: builder.query({
      query: (limit = 10) => `per_page=${limit}`,
    }),
  }),
});

export const { useGetPhotosQuery } = api;


Solution

  • query: (limit = 10) => `per_page=${limit}`,
    

    is just short for

    query: (limit = 10) => ({
      url: `per_page=${limit}`,
      method: 'GET'
    }),
    

    you probably want to do something like

    query: (limit = 10) => ({
      params: { per_page: limit }
    }),