Skip to content

Commit

Permalink
Add staleTime
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvasqm committed Apr 5, 2024
1 parent 9aafc70 commit 1a11dda
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
28 changes: 16 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"@emotion/styled": "^11.11.0",
"@tanstack/react-query": "^5.28.4",
"@tanstack/react-query-devtools": "^5.28.4",
"@types/ms": "^0.7.34",
"axios": "^1.5.0",
"framer-motion": "^10.16.4",
"ms": "^2.1.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.3",
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/usePokemon.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useQuery } from "@tanstack/react-query";
import Pokemon from "../models/Pokemon";
import APIClient from "../services/apiClient";
import ms from "ms";

const client = new APIClient<Pokemon>('/pokemon');

const usePokemon = (id: number) => useQuery<Pokemon, Error>({
queryKey: ['pokemon', id],
queryFn: () => client.get(id),
staleTime: ms('24h')
})

export default usePokemon;
2 changes: 2 additions & 0 deletions src/hooks/usePokemonByUrl.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useQuery } from "@tanstack/react-query";
import Pokemon from "../models/Pokemon";
import APIClient from "../services/apiClient";
import ms from "ms";

const client = new APIClient<Pokemon>('/api/pokemon');

const usePokemonByUrl = (url: string) => useQuery<Pokemon, Error>({
queryKey: ['pokemonUrl', url],
queryFn: () => client.find(url),
staleTime: ms('24h')
})

export default usePokemonByUrl;
4 changes: 3 additions & 1 deletion src/hooks/usePokemons.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useQuery } from "@tanstack/react-query";
import APIClient from "../services/apiClient";
import PokemonResult from "../models/PokemonResult";
import ms from "ms";

const client = new APIClient<PokemonResult>('/pokemon')

const usePokemons = () => useQuery({
queryKey: ['pokemons'],
queryFn: client.getAll
queryFn: client.getAll,
staleTime: ms('24h')
})

export default usePokemons

0 comments on commit 1a11dda

Please sign in to comment.