简体   繁体   English

使用 React Router v6 beta 延迟加载

[英]Lazy loading with react router v6 beta

This is my app.js using react router dom v6.0.0 beta.这是我的 app.js 使用 react router dom v6.0.0 beta。

const App = () => {
  const routing = useRoutes(routes)
  return (
    <AuthProvider>
      <MuiThemeProvider theme={theme}>
        <GlobalStyles />
        {routing}
      </MuiThemeProvider>
    </AuthProvider>
  )
}

And routes are in routes.js.路线在 routes.js 中。

const routes = [
  {
    path: "/",
    element: <LoggedOutLayout />,
    children: [
      {
        path: "auth",
        element: <AuthLayout />,
        children: [
          {
            path: "/register",
            element: <Register />,
          },
          {
            path: "/login",
            element: <Login />,
          },
          { path: "", element: <Navigate to="/auth/login" /> },
          { path: "/", element: <Navigate to="/auth/login" /> },
        ],
      },
      { path: "404", element: <NotFound /> },
      { path: "*", element: <Navigate to="/404" /> },
      { path: "", element: <Navigate to="/app/dashboard" /> },
      { path: "/", element: <Navigate to="/app/dashboard" /> },
]
.......

How do i implement lazy loading in v6?我如何在 v6 中实现延迟加载? I'm thinking React.Suspence in App.js and React.lazy in routes.js.我在考虑 App.js 中的 React.Suspence 和 routes.js 中的 React.lazy。 Any feedback is appreciated.任何反馈表示赞赏。 Thanks谢谢

It's Probably too late to answer your question, you might already find out how to implement code splitting by route with react and the latest react-router-dom v6 api.现在回答您的问题可能为时已晚,您可能已经了解如何使用 react 和最新的 react-router-dom v6 api 通过路由实现代码拆分。

But I'll try to answer this question anyway.但无论如何我都会尝试回答这个问题。

I just happened to using the v6 on another project I had the other day, and here's my approach.我只是碰巧在前几天的另一个项目中使用了 v6,这是我的方法。

Firstable, just follow the guide from react's official documentation on React.lazy section, and import your pages (the element part of routes)...首先,只需按照React.lazy部分 的 React 官方文档中的指南进行操作,然后导入您的页面(路由的元素部分)...

const { Lazy } from 'react';
const LoggedOutLayout = Lazy(() => import('../your/path/to/LoggedOutLayout'));
...
// I'll skip the others, you know the drill.

const routes = [
  {
    path: "/",
    element: <LoggedOutLayout />,
    children: [
      {
        path: "auth",
        element: <AuthLayout />,
        children: [
          {
            path: "/register",
            element: <Register />,
          },
          {
            path: "/login",
            element: <Login />,
          },
          { path: "", element: <Navigate to="/auth/login" /> },
          { path: "/", element: <Navigate to="/auth/login" /> },
        ],
      },
      { path: "404", element: <NotFound /> },
      { path: "*", element: <Navigate to="/404" /> },
      { path: "", element: <Navigate to="/app/dashboard" /> },
      { path: "/", element: <Navigate to="/app/dashboard" /> },
]
.......

Secondly, I call useRoutes() in a function component, I name it as Routes in my case.其次,我在 function 组件中调用了useRoutes() ,在本例中我将其命名为 Routes。

const Routes = () => {
  const elements = useRoutes(routes);

  return elements;
}

Then, include this component in App.js , and wrap it with <Suspense/>然后,将这个组件包含在App.js中,并用<Suspense/>包裹起来

const App = () => {
  return (
    <AuthProvider>
      <MuiThemeProvider theme={theme}>
        <GlobalStyles />
        <BrowserRouter>
          <Suspense fallback={<div>Loading...</div>}>
            <Routes/>
          </Suspense>
        </BrowserRouter>
      </MuiThemeProvider>
    </AuthProvider>
  )
}

And done.并做了。

I'm not sure if you can wrap {element} directly with <Suspense/> , I haven't test it yet.我不确定你是否可以用<Suspense/>直接包装{element} ,我还没有测试过。 But that's something you can try.但这是你可以尝试的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM