Skip to content

Instantly share code, notes, and snippets.

@kivircik-parantez
Created January 8, 2023 17:58
Show Gist options
  • Save kivircik-parantez/ed70383c9a98db97910c6ebde4f1a943 to your computer and use it in GitHub Desktop.
Save kivircik-parantez/ed70383c9a98db97910c6ebde4f1a943 to your computer and use it in GitHub Desktop.
Create an API Slice
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import type { Pokemon } from './types'
// Define a service using a base URL and expected endpoints
export const pokemonApi = createApi({
reducerPath: 'pokemonApi',
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getPokemonByName: builder.query<Pokemon, string>({
query: (name) => `pokemon/${name}`,
}),
}),
})
// Export hooks for usage in functional components, which are
// auto-generated based on the defined endpoints
export const { useGetPokemonByNameQuery } = pokemonApi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment