我想对特定路由应用延迟加载。我想要这样的东西:
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;
但这行不通正确的方法是什么?
1条答案
按热度按时间5kgi1eie1#
只有
Route
和React.Fragment
组件是Routes
组件的有效子组件。Suspense
需要在其他地方渲染。您可以在
Suspense
组件中 Package 单个路由:创建包含延迟加载的嵌套管线组件的布局管线:
或者在ReactTree中将
Suspense
组件提升到Routes
组件之外: