-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathApp.tsx
104 lines (93 loc) · 2.73 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
import * as React from "react";
import { Routes, Route, Outlet, Link } from "react-router-dom";
const About = React.lazy(() => import("./pages/About"));
const Dashboard = React.lazy(() => import("./pages/Dashboard"));
export default function App() {
return (
<div>
<h1>Lazy Loading Example</h1>
<p>
This example demonstrates how to lazily load both route elements and
even entire portions of your route hierarchy on demand. 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" page is not loaded until you click on the link. When you do,
a <code><React.Suspense fallback></code> renders while the code is
loaded via a dynamic <code>import()</code> statement. Once the code
loads, the fallback is replaced by the actual code for that page.
</p>
<p>
The "Dashboard" page does the same thing, but takes it even one step
further by <em>dynamically defining additional routes</em> once the page
loads! Since React Router lets you declare your routes as
<code><Route></code> elements, you can easily define more routes
by placing an additional <code><Routes></code> element anywhere
further down the element tree. Just be sure the parent route ends with a{" "}
<code>*</code> like <code><Route path="dashboard/*"></code> in
this case.
</p>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route
path="about"
element={
<React.Suspense fallback={<>...</>}>
<About />
</React.Suspense>
}
/>
<Route
path="dashboard/*"
element={
<React.Suspense fallback={<>...</>}>
<Dashboard />
</React.Suspense>
}
/>
<Route path="*" element={<NoMatch />} />
</Route>
</Routes>
</div>
);
}
function Layout() {
return (
<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>
);
}