Defining a Basic Fetch Users Endpoint
Demonstrate how to define a 'fetchUsers' query endpoint using the `createApi` function provided by RTK Query, showcasing basic endpoint configuration options and fetchBaseQuery usage.
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
export const usersApi = createApi({
reducerPath: 'usersApi',
baseQuery: fetchBaseQuery({ baseUrl: '/api' }),
endpoints: (builder) => ({
fetchUsers: builder.query({
query: () => '/users'
})
})
});
export const { useFetchUsersQuery } = usersApi;
This code imports the createApi and fetchBaseQuery functions from RTK Query and defines a new API slice named 'usersApi'. The slice is configured with a base URL '/api', and it includes a 'fetchUsers' query endpoint to retrieve user data from the '/users' path. Additionally, a React hook called 'useFetchUsersQuery' is auto-generated for this endpoint, which can be used to fetch user data from a React component.