1. 3
    Define an RTK Query API with createApi
    2m 14s

Define an RTK Query API with createApi

InstructorJamund Ferguson

Share this video with your friends

Send Tweet

In order to benefit from RTK Query you need to centrally define an "API slice", where you declare all of the API endpoints your application will need to use. Your redux store also has to be told about this API slice, luckily it's all only a few lines of code.

export const api = createApi({
  baseQuery: fetchBaseQuery({ baseUrl: '/api' }),
  endpoints: (builder) => ({})
})

And then later in your store file:

export const store = configureStore({
  reducer: {
    [api.reducerPath]: api.reducer,
  },
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware().concat(api.middleware),
});

One of the concepts we mention only briefly in the lesson is the base query. There are several options for base queries including some examples for axios and graphQL base queries you can look into. They are extremely customizable, but I've found using the simple fetchBaseQuery is usually enough to get started. https://redux-toolkit.js.org/rtk-query/usage/customizing-queries