Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

index.tsx

53 lines1.4 KB

1import { Link, createFileRoute } from '@tanstack/react-router'
2import { useQuery } from '@tanstack/react-query'
3import { moviesQueries } from '../services/movies/queries'
4​
5export const Route = createFileRoute('/')({
6 component: List,
7})
8​
9function List() {
10 const moviesQuery = useQuery(moviesQueries.list())
11​
12 if (moviesQuery.isLoading) {
13 return 'Loading...'
14 }
15​
16 if (moviesQuery.data) {
17 return (
18 <div>
19 <h1>Movies</h1>
20 <p>
21 Try to mock offline behavior with the button in the devtools. You can
22 navigate around as long as there is already data in the cache. You'll
23 get a refetch as soon as you go online again.
24 </p>
25 <ul>
26 {moviesQuery.data.movies.map((movie) => (
27 <li key={movie.id}>
28 <Link to="/$movieId" params={{ movieId: movie.id }}>
29 {movie.title}
30 </Link>
31 </li>
32 ))}
33 </ul>
34 <div>
35 Updated at: {new Date(moviesQuery.data.ts).toLocaleTimeString()}
36 </div>
37 <div>{moviesQuery.isFetching && 'fetching...'}</div>
38 </div>
39 )
40 }
41​
42 if (moviesQuery.isError) {
43 return (
44 <div>
45 <h1>Couldn't load movies!</h1>
46 <div>{moviesQuery.error.message}</div>
47 </div>
48 )
49 }
50​
51 // query will be in 'idle' fetchStatus while restoring from localStorage
52 return null