简体   繁体   English

使用 react-router v6 在私有路由内创建路由

[英]Creating routes inside a private route with react-router v6

Currently using react-router-dom 6.1.1 and I'm working with a private route .目前使用react-router-dom 6.1.1并且我正在使用private route

Inside this private route I usually had other routes (so that I can keep my Sidebar on them).在这条private route中,我通常有其他路线(这样我就可以将Sidebar放在它们上面)。

My code looks like this我的代码看起来像这样

// App.tsx

const RequireAuth: React.FC<PrivateRouteProps> = ({ children, redirectTo }) => {
  const isAuthenticated = Auth.isLogedIn()
  return isAuthenticated ? children : <Navigate to={redirectTo} />
}

const Page = () => {
  return (
    <div className={css.host}>
      <BrowserRouter>
        <Routes>
          <Route path="/login" element={<Login />} />

          <Route
            path="/"
            element={
              <RequireAuth redirectTo="/login">
                <Home />
              </RequireAuth>
            }
          />
        </Routes>
      </BrowserRouter>
    </div>
  )
}
// Home/index.tsx

const Home = () => {
  return (
    <div className={css.host}>
      <Sidebar sections={sidebarOptions(t)} />

      <Routes>
        {routes.map(({ breadCrumbtitle, link, component }, index) => (
          <Route path={link} key={index}>
            {component ? component : <p>[{breadCrumbtitle}] To be done</p>}
          </Route>
        ))}
      </Routes>
    </div>
  )
}

So... This setup worked with v5 but it seems to be something that doesn't really work with v6 .所以......这个设置适用于v5但它似乎不适用于v6 What can I do if I still want to keep the Sidebar for all the routes once I'm logged in?如果我在登录后仍想保留所有路线的Sidebar ,我该怎么办?

As far as I can tell the only issue is with the routes mapping, the Route components have invalid children, ie you are rendering another Route or React.Fragment as a child.据我所知,唯一的问题是routes映射, Route组件具有无效的子级,即您正在渲染另一个RouteReact.Fragment作为子级。

Move this up to the element prop of the mapped Route components.将它上移到映射的Route组件的element属性。

const Home = () => {
  return (
    <div className={css.host}>
      <Sidebar sections={sidebarOptions(t)} />
      <Routes>
        {routes.map(({ breadCrumbtitle, link, component }, index) => (
          <Route
            path={link}
            key={index}
            element={component || <p>[{breadCrumbtitle}] To be done</p>}
          />
        ))}
      </Routes>
    </div>
  );
};

编辑 created-routes-inside-a-private-route-with-react-router-v6

I ended up finding the solution to my issue.我最终找到了解决我的问题的方法。 Doing what Drew Reese suggested only worked to a certain point since I was being led to a route that, for react router , didn't exist.做 Drew Reese 建议的事情只在一定程度上起作用,因为我被引导到一条对于react router不存在的路线。

For it to work I add to do为了让它工作,我添加做

// App.tsx

const RequireAuth: React.FC<PrivateRouteProps> = ({ children, redirectTo }) => {
  const isAuthenticated = Auth.isLogedIn()
  return isAuthenticated ? children : <Navigate to={redirectTo} />
}

const Page = () => {
  return (
    <div className={css.host}>
      <BrowserRouter>
        <Routes>
          <Route path="/login" element={<Login />} />

          <Route
            path=""
            element={
              <RequireAuth redirectTo="/login">
                <Home />
              </RequireAuth>
            }
          >
            {routes.map(({ breadCrumbtitle, link, component }, index) => {
              return <Route path={link} key={index} element={component}></Route>
            })}
          </Route>
        </Routes>
      </BrowserRouter>
    </div>
  )
}
// Home/index.tsx

const Home = () => {
  return (
    <div className={css.host}>
      <Sidebar sections={sidebarOptions(t)} />

      <div className={css.contentContainer}>
        <Outlet />
      </div>
    </div>
  )
}

Using the Outlet seemed to be essential, don't know if it's something new on react router v6 but seemed to do the trick!使用Outlet似乎是必不可少的,不知道它是否是react router v6上的新东西,但似乎可以解决问题!

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

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