1
1
import * as React from "react" ;
2
- import "./App.css" ;
3
2
4
3
import {
5
4
useQuery ,
6
5
QueryClient ,
7
6
MutationCache ,
8
- useMutation ,
9
7
onlineManager ,
8
+ useIsHydrating ,
10
9
} from "react-query" ;
11
10
import { ReactQueryDevtools } from "react-query/devtools" ;
12
11
import toast , { Toaster } from "react-hot-toast" ;
@@ -21,7 +20,7 @@ import {
21
20
useMatch ,
22
21
} from "@tanstack/react-location" ;
23
22
24
- import * as api from "api" ;
23
+ import * as api from "./ api" ;
25
24
import { movieKeys , useMovie } from "./movies" ;
26
25
27
26
const persister = createWebStoragePersister ( {
@@ -70,37 +69,44 @@ export default function App() {
70
69
} ) ;
71
70
} }
72
71
>
73
- < Router
74
- location = { location }
75
- routes = { [
76
- {
77
- path : "/" ,
78
- element : < List /> ,
79
- } ,
80
- {
81
- path : ":movieId" ,
82
- element : < Detail /> ,
83
- errorElement : < MovieError /> ,
84
- loader : ( { params : { movieId } } ) =>
85
- queryClient . getQueryData ( movieKeys . detail ( movieId ) ) ??
86
- // do not load if we are offline because it returns a promise that is pending until we go online again
87
- // we just let the Detail component handle it
88
- ( onlineManager . isOnline ( )
89
- ? queryClient . fetchQuery ( movieKeys . detail ( movieId ) , ( ) =>
90
- api . fetchMovie ( movieId )
91
- )
92
- : undefined ) ,
93
- } ,
94
- ] }
95
- >
96
- < Outlet />
97
- < Toaster />
98
- </ Router >
72
+ < Movies />
99
73
< ReactQueryDevtools initialIsOpen />
100
74
</ PersistQueryClientProvider >
101
75
) ;
102
76
}
103
77
78
+ function Movies ( ) {
79
+ const isHydrating = useIsHydrating ( ) ;
80
+ return (
81
+ < Router
82
+ location = { location }
83
+ routes = { [
84
+ {
85
+ path : "/" ,
86
+ element : < List /> ,
87
+ } ,
88
+ {
89
+ path : ":movieId" ,
90
+ element : < Detail /> ,
91
+ errorElement : < MovieError /> ,
92
+ loader : ( { params : { movieId } } ) =>
93
+ queryClient . getQueryData ( movieKeys . detail ( movieId ) ) ??
94
+ // do not load if we are offline or hydrating because it returns a promise that is pending until we go online again
95
+ // we just let the Detail component handle it
96
+ ( onlineManager . isOnline ( ) && ! isHydrating
97
+ ? queryClient . fetchQuery ( movieKeys . detail ( movieId ) , ( ) =>
98
+ api . fetchMovie ( movieId )
99
+ )
100
+ : undefined ) ,
101
+ } ,
102
+ ] }
103
+ >
104
+ < Outlet />
105
+ < Toaster />
106
+ </ Router >
107
+ ) ;
108
+ }
109
+
104
110
function List ( ) {
105
111
const moviesQuery = useQuery ( movieKeys . list ( ) , api . fetchMovies ) ;
106
112
0 commit comments