Created
January 8, 2023 17:58
-
-
Save kivircik-parantez/ed70383c9a98db97910c6ebde4f1a943 to your computer and use it in GitHub Desktop.
Create an API Slice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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