简体   繁体   English

如何在 react-router-dom 中创建条件路由?

[英]How to make conditional routes in react-router-dom?

I am making an application using react-router-dom for redirections, I have wrapped all the <Routes> inside of a container (which is given by Material UI), now, I want my home page to be outside of container (as I want to display a hero image and wrapping it inside of the container brings unnecessary padding from sides, which I don't really want ).我正在制作一个使用 react-router-dom 进行重定向的应用程序,我已经将所有<Routes>包装在一个容器中(由 Material UI 提供),现在,我希望我的主页在容器之外(因为我想要显示英雄形象并将其包裹在容器内会从侧面带来不必要的填充,我真的不想要)。 But, I want to keep all the other inside of the container.但是,我想将所有其他内容保留在容器内。

I am using react-router-dom v6.我正在使用 react-router-dom v6。

inside of my App.tsx function -在我的 App.tsx function 中 -

function App() {

return (

// I want this page (i.e PageH) not be wrapped around the (container - from MUI)
<Routes>
<Route exact path = '/pageH' component = {PageH} />
</Routes>

 <Container maxWidth = {'lg'} > //from MUI (only want below pages within container)
   <Routes>
     <Route path='/' element={PageA} /> //home page path - default
     <Route path='/pageB' element= {<PageB />} />
     <Route path='/pageC' element={<PageC />} />
     <Route path='/pageD' element={<PageD />} />
     <Route path='/pageE' element={<PageE />} />
     <Route path = '*' element = {<NotFound />} />
</Routes>
 </Container>
 )};

export default App;导出默认应用程序;

Now here, as you can see I have created 2现在在这里,如你所见,我已经创建了 2

<Routes>
<Route ... > // to pageH
</Routes>

<Routes> // to other pages
<Container>
<Route ... > 
<Route ... >
</Container>
...
</Routes>

wrappers where first wrapper renders those pages where I don't want a container, and second wrapper rendering the rest of application pages where I need a container wrapper around.包装器,其中第一个包装器呈现我不需要容器的那些页面,第二个包装器呈现我需要容器包装器的应用程序页面的 rest。

The functionality I implemented is working fine, but the problem is ( on those pages which are inside of my first Routes wrapper, the <NotFoundPage /> is also getting rendered within those pages).我实现的功能工作正常,但问题是(在我的第一个路由包装器内的那些页面上, <NotFoundPage />也在这些页面内呈现)。

How to separate my NotFound, page from each one of the page, and only render when none of the pages, either from <Routes /> for 1st group or <Routes /> for second group doesn't matches?如何将我的 NotFound 页面与每个页面分开,并且仅在没有页面时呈现,第一组的<Routes /> <Routes />第二组的 <Routes /> 不匹配?

An answer to this would also clear all the doubts of making hero images to those pages which are indeed inside of the container, but just for that particular page, the container wrapper needs to get removed.对此的回答也将消除对确实在容器内的那些页面制作英雄图像的所有疑虑,但仅对于该特定页面,容器包装器需要被删除。

One alternate way is to make every component separately be wrapped inside of the container, but that would increase unnecessary lines of code and a lot of a work...另一种方法是让每个组件单独包装在容器内,但这会增加不必要的代码行和大量工作......

I want to implement a logic at the root level only (ie my App.tsx component, where routes to all my other components exits.)我只想在根级别实现一个逻辑(即我的App.tsx组件,其中存在到我所有其他组件的路由。)

All Suggestions are welcome: )欢迎所有建议:)

Create layout routes/components for the routes that need a different UI container to be rendered into.为需要渲染不同 UI 容器的路由创建布局路由/组件。 Layout route components render an Outlet for nested routes to render their content into.布局路由组件为嵌套路由渲染一个Outlet以将其内容渲染到其中。

Example:例子:

import { Routes, Route, Outlet } from 'react-router-dom';

const Layout = () => (
  <Container maxWidth="lg">
    <Outlet />
  </Container>
);

function App() {
  return (
    <Routes>
      <Route path="/pageH" element={<PageH />} />
      <Route element={<Layout />}>
        <Route path="/" element={<PageA />} />
        <Route path="/pageB" element={<PageB />} />
        <Route path="/pageC" element={<PageC />} />
        <Route path="/pageD" element={<PageD />} />
        <Route path="/pageE" element={<PageE />} />
        <Route path="*" element={<NotFound />} />
      </Route
    </Routes>
  );
};

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

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