-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.tsx
100 lines (90 loc) · 2.41 KB
/
demo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as React from "react";
import { createRoot } from "react-dom/client";
import {
DataBrowserRouter,
Form,
Link,
Outlet,
redirect,
Route,
useLocation,
useNavigation,
} from "react-router-dom";
import type { CallbackData } from "remix-use-spa-metrics";
import { useSpaMetrics } from "remix-use-spa-metrics";
createRoot(document.getElementById("app")).render(
<React.StrictMode>
<DataBrowserRouter fallbackElement={<p>Loading...</p>}>
<Route path="/" element={<Root />}>
<Route index loader={indexLoader} element={<Index />} />
<Route
path="page"
loader={pageLoader}
action={pageAction}
element={<Page />}
/>
</Route>
</DataBrowserRouter>
</React.StrictMode>
);
async function sleep() {
await new Promise((r) => setTimeout(r, Math.round(Math.random() * 1000)));
}
async function indexLoader() {
await sleep();
return {};
}
function Index() {
return <h2>Index Page</h2>;
}
async function pageLoader({ request }) {
await sleep();
let isRedirect = new URL(request.url).searchParams.get("redirect") === "1";
if (isRedirect) {
return redirect("/");
}
return {};
}
async function pageAction({ request }) {
await sleep();
let isRedirect = (await request.formData()).get("redirect") === "1";
if (isRedirect) {
return redirect("/");
}
return {};
}
function Page() {
return <h2>Page</h2>;
}
function Root() {
let location = useLocation();
let navigation = useNavigation();
let callback = React.useCallback((data: CallbackData) => {
// Send data to your third-party service here
console.log(data);
}, []);
useSpaMetrics(location, navigation, callback);
return (
<>
<style>{`
a, button {
display: block;
margin-bottom: 0.5rem;
}
`}</style>
<h1>Root Layout</h1>
<p>navigation.state: {navigation.state}</p>
<Link to="/">Home (async loading navigation)</Link>
<Link to="/page">Page (async loading navigation)</Link>
<Link to="/page?redirect=1">Page (async redirect navigation)</Link>
<Form method="post" action="page">
<button type="submit">Submit (async submission navigation)</button>
</Form>
<Form method="post" action="page">
<input type="hidden" name="redirect" value="1" />
<button type="submit">Submit (async redirect navigation)</button>
</Form>
<Outlet />
</>
);
}