Skip to content

Instantly share code, notes, and snippets.

@xtiannyeto
Last active February 15, 2022 13:11
Show Gist options
  • Save xtiannyeto/b5b5a298d7316604bf3e327955654641 to your computer and use it in GitHub Desktop.
Save xtiannyeto/b5b5a298d7316604bf3e327955654641 to your computer and use it in GitHub Desktop.
RTK query starter
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { 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