在嵌套路由中实现延迟加载
Implement lazy loading in nested routes
我正在尝试在 React 应用程序中添加实现延迟加载,它在 top-level 路由中运行良好。但我也想要 <Suspense></Suspense>
并为嵌套路由提供后备。根据react-router-dom,Routes
的children必然只有Fragment
或Route
。有什么办法可以解决这个问题吗?
代码如下:
import { lazy, Fragment, Suspense } from "react";
import { BrowserRouter, Route, Routes, Navigate } from "react-router-dom";
import Header from "./components/Header";
const Home = lazy(() => import("./components/layout/Home"));
const Destination = lazy(() => import("./components/layout/Destination"));
const Planet = lazy(() => import("./components/Planet"));
const Crew = lazy(() => import("./components/layout/Crew"));
const Biography = lazy(() => import("./components/Biography"));
const Technology = lazy(() => import("./components/layout/Technology"));
const Procedure = lazy(() => import("./components/Procedure"));
function App() {
return (
<BrowserRouter>
<Header />
<Suspense fallback={<p className='fixed left-1/2 top-1/2 -translate-x-1/2 text-2xl'>Loading ...</p>}>
<Routes>
<Route path="/" element={<Navigate to="home" />} />
<Route path="home" element={<Home />} />
<Route path="destination" element={<Destination />}>
<Route path=":id/:planet" element={<Planet />} />
</Route>
<Route path="crew" element={<Crew />}>
<Route path=":id" element={<Biography />} />
</Route>
<Route path="technology" element={<Technology />}>
<Route path=":id" element={<Procedure />} />
</Route>
</Routes>
</Suspense>
</BrowserRouter>
);
}
export default App;
您可以将各个路由组件包装在它们自己的 Suspense
中。
示例:
<BrowserRouter>
<Header />
<Suspense
fallback={(
<p className='fixed left-1/2 top-1/2 -translate-x-1/2 text-2xl'>
Loading ...
</p>
)}
>
<Routes>
...
<Route path="destination" element={<Destination />}>
<Route
path=":id/:planet"
element={(
<Suspense fallback={<>Loading Planet ...</>}>
<Planet />
</Suspense>
)}
/>
</Route>
...
</Routes>
</Suspense>
</BrowserRouter>
我正在尝试在 React 应用程序中添加实现延迟加载,它在 top-level 路由中运行良好。但我也想要 <Suspense></Suspense>
并为嵌套路由提供后备。根据react-router-dom,Routes
的children必然只有Fragment
或Route
。有什么办法可以解决这个问题吗?
代码如下:
import { lazy, Fragment, Suspense } from "react";
import { BrowserRouter, Route, Routes, Navigate } from "react-router-dom";
import Header from "./components/Header";
const Home = lazy(() => import("./components/layout/Home"));
const Destination = lazy(() => import("./components/layout/Destination"));
const Planet = lazy(() => import("./components/Planet"));
const Crew = lazy(() => import("./components/layout/Crew"));
const Biography = lazy(() => import("./components/Biography"));
const Technology = lazy(() => import("./components/layout/Technology"));
const Procedure = lazy(() => import("./components/Procedure"));
function App() {
return (
<BrowserRouter>
<Header />
<Suspense fallback={<p className='fixed left-1/2 top-1/2 -translate-x-1/2 text-2xl'>Loading ...</p>}>
<Routes>
<Route path="/" element={<Navigate to="home" />} />
<Route path="home" element={<Home />} />
<Route path="destination" element={<Destination />}>
<Route path=":id/:planet" element={<Planet />} />
</Route>
<Route path="crew" element={<Crew />}>
<Route path=":id" element={<Biography />} />
</Route>
<Route path="technology" element={<Technology />}>
<Route path=":id" element={<Procedure />} />
</Route>
</Routes>
</Suspense>
</BrowserRouter>
);
}
export default App;
您可以将各个路由组件包装在它们自己的 Suspense
中。
示例:
<BrowserRouter>
<Header />
<Suspense
fallback={(
<p className='fixed left-1/2 top-1/2 -translate-x-1/2 text-2xl'>
Loading ...
</p>
)}
>
<Routes>
...
<Route path="destination" element={<Destination />}>
<Route
path=":id/:planet"
element={(
<Suspense fallback={<>Loading Planet ...</>}>
<Planet />
</Suspense>
)}
/>
</Route>
...
</Routes>
</Suspense>
</BrowserRouter>