英文:
How to selectively apply lazy loading and suspense to specific routes with React Router?
问题
I want to apply lazy loading to specific routes. 我想对特定的路由应用延迟加载。
英文:
I want apply lazy loading to specific routes. I want something like this:
import { lazy, Suspense } from "react";
import { Route, Routes } from "react-router-dom";
import NotLazyComponent from "./NotLazyComponent";
const LazyOne = lazy(() => import("./LazyOne"));
const LazyTwo = lazy(() => import("./LazyTwo"));
const App = () => {
return (
<Routes>
<Route path="/not-lazy" element={<NotLazyComponent />} />
<Suspense fallback={<div>Loading...</div>}>
<Route path="/lazy-one" element={<LazyOne />} />
<Route path="/lazy-two" element={<LazyTwo />} />
</Suspense>
</Routes>
);
};
export default App;
but this won't work. What is the correct way to do that?
答案1
得分: 0
只有Route
和React.Fragment
组件是Routes
组件的有效子元素。 Suspense
需要在其他地方渲染。
您可以在Suspense
组件中包装单独的路由:
const App = () => {
return (
<Routes>
<Route path="/not-lazy" element={<NotLazyComponent />} />
<Route
path="/lazy-one"
element={(
<Suspense fallback={<div>Loading...</div>}>
<LazyOne />
</Suspense>
)}
/>
<Route
path="/lazy-two"
element={(
<Suspense fallback={<div>Loading...</div>}>
<LazyTwo />
</Suspense>
)}
/>
</Routes>
);
};
创建一个包装延迟加载的嵌套路由组件的布局路由:
import { Routes, Route, Outlet } from 'react-router-dom';
const SuspenseLayout = () => (
<Suspense fallback={<div>Loading...</div>}>
<Outlet />
</Suspense>
);
const App = () => {
return (
<Routes>
<Route path="/not-lazy" element={<NotLazyComponent />} />
<Route element={<SuspenseLayout />}>
<Route path="/lazy-one" element={<LazyOne />} />
<Route path="/lazy-two" element={<LazyTwo />} />
</Route>
</Routes>
);
};
或者将Suspense
组件提升到Routes
组件外部的React树中:
const App = () => {
return (
<Suspense fallback={<div>Loading...</div>}>
<Routes>
<Route path="/not-lazy" element={<NotLazyComponent />} />
<Route path="/lazy-one" element={<LazyOne />} />
<Route path="/lazy-two" element={<LazyTwo />} />
</Routes>
</Suspense>
);
};
英文:
Only the Route
and React.Fragment
components are valid children of the Routes
component. Suspense
needs to be rendered elsewhere.
You could wrap individual routes in the Suspense
component:
const App = () => {
return (
<Routes>
<Route path="/not-lazy" element={<NotLazyComponent />} />
<Route
path="/lazy-one"
element={(
<Suspense fallback={<div>Loading...</div>}>
<LazyOne />
</Suspense>
)}
/>
<Route
path="/lazy-two"
element={(
<Suspense fallback={<div>Loading...</div>}>
<LazyTwo />
</Suspense>
)}
/>
</Routes>
);
};
Create a layout route that wraps the lazily loaded nested route components:
import { Routes, Route, Outlet } from 'react-router-dom';
const SuspenseLayout = () => (
<Suspense fallback={<div>Loading...</div>}>
<Outlet />
</Suspense>
);
const App = () => {
return (
<Routes>
<Route path="/not-lazy" element={<NotLazyComponent />} />
<Route element={<SuspenseLayout />}>
<Route path="/lazy-one" element={<LazyOne />} />
<Route path="/lazy-two" element={<LazyTwo />} />
</Route>
</Routes>
);
};
Or lift the Suspense
component higher in the ReactTree outside the Routes
component:
const App = () => {
return (
<Suspense fallback={<div>Loading...</div>}>
<Routes>
<Route path="/not-lazy" element={<NotLazyComponent />} />
<Route path="/lazy-one" element={<LazyOne />} />
<Route path="/lazy-two" element={<LazyTwo />} />
</Routes>
</Suspense>
);
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论