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

list.tsx

47 lines1.2 KB

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