Skip to content

Commit 6e4ebec

Browse files
authored
docs(examples): fix offline example (#3463)
* docs(examples): remove non-existing / unused imports * docs(examples): add missing msw dependency and fix some more things
1 parent 3ed5e48 commit 6e4ebec

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

examples/offline/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"@tanstack/react-location": "^3.7.0",
1111
"ky": "^0.30.0",
12+
"msw": "^0.39.2",
1213
"react": "^17.0.2",
1314
"react-dom": "^17.0.2",
1415
"react-hot-toast": "^2.2.0",

examples/offline/src/App.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import * as React from "react";
2-
import "./App.css";
32

43
import {
54
useQuery,
65
QueryClient,
76
MutationCache,
8-
useMutation,
97
onlineManager,
8+
useIsHydrating,
109
} from "react-query";
1110
import { ReactQueryDevtools } from "react-query/devtools";
1211
import 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";
2524
import { movieKeys, useMovie } from "./movies";
2625

2726
const 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+
104110
function List() {
105111
const moviesQuery = useQuery(movieKeys.list(), api.fetchMovies);
106112

examples/offline/src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3-
import "./index.css";
43
import App from "./App";
54
import { worker } from "./api";
65

0 commit comments

Comments
 (0)