我正在尝试在 React 应用程序中添加实现延迟加载,这在顶级路由中运行良好。但我也想要
<Suspense></Suspense>
并为嵌套路由提供后备。根据 react-router-dom,必须只有 Fragment
或 Route
作为 Routes
的孩子。有什么办法可以解决这个问题吗?
代码如下:
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>
您也可以使用
@loadable/component
。这是有关如何使用它的说明。
首先,将
@loadable/component
包添加到您的项目中。
然后你可以把你的代码改成这样。
import { Fragment, Suspense } from "react";
import { BrowserRouter, Route, Routes, Navigate } from "react-router-dom";
import loadable from '@loadable/component';
import Header from "./components/Header";
const Home = loadable(() => import("./components/layout/Home"));
const Destination = loadable(() => import("./components/layout/Destination"));
const Planet = loadable(() => import("./components/Planet"));
const Crew = loadable(() => import("./components/layout/Crew"));
const Biography = loadable(() => import("./components/Biography"));
const Technology = loadable(() => import("./components/layout/Technology"));
const Procedure = loadable(() => 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;