React.lazy 和自定义 protectedRoute 与 router-react-dom,如何让它工作?

发布于 2025-01-16 14:30:50 字数 2091 浏览 0 评论 0原文

我是 React 新手,正在尝试 React.lazy 和 Suspense 导入,我不得不说,我喜欢它们!我的网站性能从 45% 提高到 50-60%,而且还没有优化图像!谷歌搜索结果,我来了!

但是,我有一个问题,我不知道如何延迟加载在我的自定义 ProtectedRoute 和 React-router-dom v5 中呈现的组件。

当我使用 React-router-doms 本机路由时,延迟加载起作用并生效,但是当我想通过自定义受保护路由中的一个加载受保护组件时,什么也没有发生,控制台或网站上没有错误消息,只是白屏。我怀疑导入和代码放置在错误的位置存在一些问题。

APP

import React, { Suspense } from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import ProtectedRoute from "./pages/middleware/ProtectedRoute";

const Login = React.lazy(() => import("./pages/Login"));
const WebsiteCRUDs = React.lazy(() => import("./pages/WebsiteCRUDs"));

function App() {
  return (
    <div className="App">
      <Router>
        <Switch>
                {/* This one works */}
          <Suspense fallback={<div>Loading</div>}>
            <Route exact path="/admin" component={Login} />
          </Suspense>
                {/* This one does NOT work */}
          <Suspense fallback={<div>Loading</div>}>
            <ProtectedRoute exact path="/admin/crud" component={WebsiteCRUDs} />
          </Suspense>
          </Switch>
      </Router>
    </div>
  );
}

export default App;

保护路由:

import React from "react";
import { Route, Redirect } from "react-router-dom";
import { useEffect, useState } from "react";

const ProtectedRoute = ({ component: Component, ...rest }) => {
  const [isAuth, setIsAuth] = useState(false);
  const [isLoading, setIsLoading] = useState(true);

  // Logic validation goes here with redirect if user is not auth.

  return (
    <Route
      {...rest}
      render={(props) =>
        isLoading ? (
          <h1>Checking Validation</h1>
        ) : isAuth ? (
          <Component {...props} />
        ) : (
          <Redirect
            to={{ pathname: "/admin", state: { from: props.location } }}
          />
        )
      }
    />
  );
};

export default ProtectedRoute;

I'm new to react and is trying out the React.lazy and Suspense imports, and I just have to say, I love them!!! My website went from 45% in performance up to 50-60% and that is without optimizing images! Google search results, here I come!

However, I have a problem, I don't know how to lazy load a component which is rendered in my custom ProtectedRoute and react-router-dom v5.

The lazy loading works and takes effect when I use the React-router-doms native Route, but when I want to load a protected component via one in my custom protected routes, nothing happens, no error message in console or on the website, just a white screen. I suspect there's some problem with the import and code being put in the wrong place.

APP

import React, { Suspense } from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import ProtectedRoute from "./pages/middleware/ProtectedRoute";

const Login = React.lazy(() => import("./pages/Login"));
const WebsiteCRUDs = React.lazy(() => import("./pages/WebsiteCRUDs"));

function App() {
  return (
    <div className="App">
      <Router>
        <Switch>
                {/* This one works */}
          <Suspense fallback={<div>Loading</div>}>
            <Route exact path="/admin" component={Login} />
          </Suspense>
                {/* This one does NOT work */}
          <Suspense fallback={<div>Loading</div>}>
            <ProtectedRoute exact path="/admin/crud" component={WebsiteCRUDs} />
          </Suspense>
          </Switch>
      </Router>
    </div>
  );
}

export default App;

ProtectedRoute:

import React from "react";
import { Route, Redirect } from "react-router-dom";
import { useEffect, useState } from "react";

const ProtectedRoute = ({ component: Component, ...rest }) => {
  const [isAuth, setIsAuth] = useState(false);
  const [isLoading, setIsLoading] = useState(true);

  // Logic validation goes here with redirect if user is not auth.

  return (
    <Route
      {...rest}
      render={(props) =>
        isLoading ? (
          <h1>Checking Validation</h1>
        ) : isAuth ? (
          <Component {...props} />
        ) : (
          <Redirect
            to={{ pathname: "/admin", state: { from: props.location } }}
          />
        )
      }
    />
  );
};

export default ProtectedRoute;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2025-01-23 14:30:51

请尝试这样

import React, { Suspense } from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import ProtectedRoute from "./pages/middleware/ProtectedRoute";

const Login = React.lazy(() => import("./pages/Login"));
const WebsiteCRUDs = React.lazy(() => import("./pages/WebsiteCRUDs"));

function App() {
  return (
    <div className="App">
      <Router>
        <Switch>
          <Suspense fallback={<div>Loading</div>}>
            <Route exact path="/admin" component={Login} />
            <ProtectedRoute exact path="/admin/crud" component={WebsiteCRUDs} />
          </Suspense>
          </Switch>
      </Router>
    </div>
  );
}

export default App;

Please try like this

import React, { Suspense } from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import ProtectedRoute from "./pages/middleware/ProtectedRoute";

const Login = React.lazy(() => import("./pages/Login"));
const WebsiteCRUDs = React.lazy(() => import("./pages/WebsiteCRUDs"));

function App() {
  return (
    <div className="App">
      <Router>
        <Switch>
          <Suspense fallback={<div>Loading</div>}>
            <Route exact path="/admin" component={Login} />
            <ProtectedRoute exact path="/admin/crud" component={WebsiteCRUDs} />
          </Suspense>
          </Switch>
      </Router>
    </div>
  );
}

export default App;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文