React Router V6 路由

互联网 行业动态 更新时间:2024-06-13 00:19:32

Mbr*_*vda 5

你不能 Route在 React Router v6 中编写 s。相反,在与其他路由相同的级别创建 Route,并在elementprop 中使用您的 PrivateRoute 组件。示例(来源):

import { Routes, Route, Navigate } from "react-router-dom";

function App() {
  return (
    <Routes>
      <Route path="/public" element={<PublicPage />} />
      <Route
        path="/protected"
        element={
          // Good! Do your position here instead of wrapping <Route>.
          // This is really just inverting the wrapping, but it's a lot
          // more clear which ponents expect which props.
          <RequireAuth redirectTo="/login">
            <ProtectedPage />
          </RequireAuth>
        }
      />
    </Routes>
  );
}

function RequireAuth({ children, redirectTo }) {
  let isAuthenticated = getAuth();
  return isAuthenticated ? children : <Navigate to={redirectTo} />;
}

更多推荐

路由,React,Router

本文发布于:2023-04-21 09:47:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/hyzx/383dec8c80995e358a50e71cd421fa10.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   React   Router

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!