11import * as React from "react" ;
2- import "./App.css" ;
32
43import {
54 useQuery ,
65 QueryClient ,
76 MutationCache ,
8- useMutation ,
97 onlineManager ,
8+ useIsHydrating ,
109} from "react-query" ;
1110import { ReactQueryDevtools } from "react-query/devtools" ;
1211import toast , { Toaster } from "react-hot-toast" ;
@@ -21,7 +20,7 @@ import {
2120 useMatch ,
2221} from "@tanstack/react-location" ;
2322
24- import * as api from "api" ;
23+ import * as api from "./ api" ;
2524import { movieKeys , useMovie } from "./movies" ;
2625
2726const persister = createWebStoragePersister ( {
@@ -70,37 +69,44 @@ export default function App() {
7069 } ) ;
7170 } }
7271 >
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 />
9973 < ReactQueryDevtools initialIsOpen />
10074 </ PersistQueryClientProvider >
10175 ) ;
10276}
10377
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+
104110function List ( ) {
105111 const moviesQuery = useQuery ( movieKeys . list ( ) , api . fetchMovies ) ;
106112
0 commit comments