如何在React Router中有选择地应用懒加载和悬念到特定路由?

huangapple go评论78阅读模式
英文:

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

只有RouteReact.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 = () =&gt; {
  return (
    &lt;Routes&gt;
      &lt;Route path=&quot;/not-lazy&quot; element={&lt;NotLazyComponent /&gt;} /&gt;
      &lt;Route
        path=&quot;/lazy-one&quot;
        element={(
          &lt;Suspense fallback={&lt;div&gt;Loading...&lt;/div&gt;}&gt;
            &lt;LazyOne /&gt;
          &lt;/Suspense&gt;
        )}
      /&gt;
      &lt;Route
        path=&quot;/lazy-two&quot;
        element={(
          &lt;Suspense fallback={&lt;div&gt;Loading...&lt;/div&gt;}&gt;
            &lt;LazyTwo /&gt;
          &lt;/Suspense&gt;
        )}
      /&gt;
    &lt;/Routes&gt;
  );
};

Create a layout route that wraps the lazily loaded nested route components:

import { Routes, Route, Outlet } from &#39;react-router-dom&#39;;

const SuspenseLayout = () =&gt; (
  &lt;Suspense fallback={&lt;div&gt;Loading...&lt;/div&gt;}&gt;
    &lt;Outlet /&gt;
  &lt;/Suspense&gt;
);

const App = () =&gt; {
  return (
    &lt;Routes&gt;
      &lt;Route path=&quot;/not-lazy&quot; element={&lt;NotLazyComponent /&gt;} /&gt;
      &lt;Route element={&lt;SuspenseLayout /&gt;}&gt;
        &lt;Route path=&quot;/lazy-one&quot; element={&lt;LazyOne /&gt;} /&gt;
        &lt;Route path=&quot;/lazy-two&quot; element={&lt;LazyTwo /&gt;} /&gt;
      &lt;/Route&gt;
    &lt;/Routes&gt;
  );
};

Or lift the Suspense component higher in the ReactTree outside the Routes component:

const App = () =&gt; {
  return (
    &lt;Suspense fallback={&lt;div&gt;Loading...&lt;/div&gt;}&gt;
      &lt;Routes&gt;
        &lt;Route path=&quot;/not-lazy&quot; element={&lt;NotLazyComponent /&gt;} /&gt;
        &lt;Route path=&quot;/lazy-one&quot; element={&lt;LazyOne /&gt;} /&gt;
        &lt;Route path=&quot;/lazy-two&quot; element={&lt;LazyTwo /&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/Suspense&gt;
  );
};

huangapple
  • 本文由 发表于 2023年5月18日 05:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定