-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
/
Copy pathApp.tsx
137 lines (124 loc) · 3.38 KB
/
App.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
Outlet,
Link,
createBrowserRouter,
RouterProvider,
useNavigation,
} from "react-router-dom";
const router = createBrowserRouter([
{
path: "/",
element: <Layout />,
children: [
{
index: true,
element: <Home />,
},
{
path: "about",
// Single route in lazy file
lazy: () => import("./pages/About"),
},
{
path: "dashboard",
async lazy() {
// Multiple routes in lazy file
let { DashboardLayout } = await import("./pages/Dashboard");
return { Component: DashboardLayout };
},
children: [
{
index: true,
async lazy() {
let { DashboardIndex } = await import("./pages/Dashboard");
return { Component: DashboardIndex };
},
},
{
path: "messages",
async lazy() {
let { dashboardMessagesLoader, DashboardMessages } = await import(
"./pages/Dashboard"
);
return {
loader: dashboardMessagesLoader,
Component: DashboardMessages,
};
},
},
],
},
{
path: "*",
element: <NoMatch />,
},
],
},
]);
export default function App() {
return <RouterProvider router={router} fallbackElement={<p>Loading...</p>} />;
}
function Layout() {
let navigation = useNavigation();
return (
<div>
<h1>Lazy Loading Example using RouterProvider</h1>
<p>
This example demonstrates how to lazily load route definitions using{" "}
<code>route.lazy()</code>. To get the full effect of this demo, be sure
to open your Network tab and watch the new bundles load dynamically as
you navigate around.
</p>
<p>
The "About" and "Dashboard" pages are not loaded until you click on the
link. When you do, the code is loaded via a dynamic{" "}
<code>import()</code> statement during the <code>loading</code> phase of
the navigation. Once the code loads, the route loader executes, and then
the element renders with the loader-provided data.
</p>
<p>
This works for all data-loading/rendering related properties of a route,
including <code>action</code>, <code>loader</code>, <code>element</code>
, <code>errorElement</code>, and <code>shouldRevalidate</code>. You
cannot return path-matching properties from <code>lazy()</code> such as{" "}
<code>path</code>, <code>index</code>, <code>children</code>, and{" "}
<code>caseSensitive</code>.
</p>
<div style={{ position: "fixed", top: 0, right: 0 }}>
{navigation.state !== "idle" && <p>Navigation in progress...</p>}
</div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/dashboard/messages">Messages (Dashboard)</Link>
</li>
</ul>
</nav>
<hr />
<Outlet />
</div>
);
}
function Home() {
return (
<div>
<h2>Home</h2>
</div>
);
}
function NoMatch() {
return (
<div>
<h2>Nothing to see here!</h2>
<p>
<Link to="/">Go to the home page</Link>
</p>
</div>
);
}