Created
February 19, 2022 04:32
-
-
Save mhaecal/0fb6b8e5b96b40c2c6db2a7846e33d96 to your computer and use it in GitHub Desktop.
RTK Query with TypeScript
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 { configureStore } from '@reduxjs/toolkit' | |
import { pokemonApi } from '../services/pokemon' | |
export const store = configureStore({ | |
reducer: { | |
[pokemonApi.reducerPath]: pokemonApi.reducer, | |
}, | |
// adding the api middleware enables caching, invalidation, polling and other features of `rtk-query` | |
middleware: (getDefaultMiddleware) => | |
getDefaultMiddleware().concat(pokemonApi.middleware), | |
}) |
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 React, { useState } from 'react' | |
import { | |
useGetPokemonByIdQuery, | |
useGetPokemonByNameQuery, | |
} from './services/pokemon' | |
function App() { | |
const [id, setId] = useState<number>(5) | |
const [name, setName] = useState<string>('bulbasaur') | |
const { | |
data: pokemonsById, | |
isLoading: loading1, | |
error: error1, | |
} = useGetPokemonByIdQuery(id) | |
const { | |
data: pokemonsByName, | |
isLoading: loading2, | |
error: error2, | |
} = useGetPokemonByNameQuery(name) | |
if (loading1 || loading2) return <div>Loading...</div> | |
if (error1 || error2) return <div>Error</div> | |
return ( | |
<div> | |
<pre>{JSON.stringify(pokemonsById)}</pre> | |
<pre>{JSON.stringify(pokemonsByName)}</pre> | |
</div> | |
) | |
} | |
export default App |
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 React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import './index.css'; | |
import App from './App'; | |
import { store } from './app/store'; | |
import { Provider } from 'react-redux'; | |
import * as serviceWorker from './serviceWorker'; | |
ReactDOM.render( | |
<React.StrictMode> | |
<Provider store={store}> | |
<App /> | |
</Provider> | |
</React.StrictMode>, | |
document.getElementById('root') | |
); | |
// If you want your app to work offline and load faster, you can change | |
// unregister() to register() below. Note this comes with some pitfalls. | |
// Learn more about service workers: https://bit.ly/CRA-PWA | |
serviceWorker.unregister(); |
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' | |
export const pokemonApi = createApi({ | |
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }), | |
endpoints: (builder) => ({ | |
// ResultType QueryArg | |
// v v | |
getPokemonById: builder.query<Pokemon, number>({ | |
// inferred as `number` from the `QueryArg` type | |
// v | |
query: (id) => 'pokemon/' + id, | |
}), | |
getPokemonByName: builder.query<Pokemon, string>({ | |
query: (name) => 'pokemon/' + name, | |
}), | |
}), | |
}) | |
export const { useGetPokemonByIdQuery, useGetPokemonByNameQuery } = pokemonApi |
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
// expected return from pokemonApi | |
export interface Pokemon { | |
abilities: [{}] | |
id: number | |
name: string | |
species: { | |
name: string | |
url: string | |
} | |
} |
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
Show hidden characters
{ | |
"compilerOptions": { | |
"target": "es5", | |
"lib": ["dom", "dom.iterable", "esnext"], | |
"allowJs": true, | |
"skipLibCheck": true, | |
"esModuleInterop": true, | |
"allowSyntheticDefaultImports": true, | |
"strict": true, | |
"forceConsistentCasingInFileNames": true, | |
"module": "esnext", | |
"moduleResolution": "node", | |
"resolveJsonModule": true, | |
"isolatedModules": true, | |
"noEmit": true, | |
"jsx": "react" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment