简体   繁体   English

React Router:如何在所有路由上渲染元素,除了一个?

[英]React Router: How to render element on all routes, except one?

I have HTML structure like this:我有这样的 HTML 结构:

<body>
  <nav>
     <!--navigation elements -->
  </nav>
  <div className='main'>
     <!--other elements -->
  </div>
  <div className='container'></div>
</body>

And routing defined like this:路由定义如下:

<Router>
  <Fragment>
    <Navbar />
    <Route exact path="/" component={Landing} />
    <div className="container">
       <Alert />
       <Switch>
           <Route exact path="/register" component={Register} />
           <Route exact path="/login" component={Login} />
           <Route exact path="/profiles" component={Profiles} />
       </Switch>
    </div>
  </Fragment>
</Router>

The "container" element is present on all routes however I do not want it to be rendered on the "/" route. “容器”元素存在于所有路由上,但我不希望它在“/”路由上呈现。

How can I stop <div className="container"> from being rendered on the "/" route?如何阻止<div className="container">"/"路由上呈现? I want it to be rendered on all other routes except of "/" .我希望它在除"/"之外的所有其他路线上呈现。

A solution I found, but don't want to use is to explicitly insert the element with class="container" in each component that is rendered in my <Switch> .我发现但不想使用的解决方案是在我的<Switch>呈现的每个组件中显式插入带有class="container"的元素。 Is there a better way?有没有更好的办法?

You should be able to achieve what you require via nested routes and a "no match route" .您应该能够通过嵌套路由“无匹配路由”实现您的需求。

The idea would be to introduce structure to your routing via nested routes, to restrict rendering of <div className="container"> to non / routes.这个想法是通过嵌套路由将结构引入路由,以限制<div className="container">渲染为非/路由。

To do this, you could extract a component (ie WithContainer ) that renders a <Route> for paths;为此,您可以提取一个为路径呈现<Route>的组件(即WithContainer ); /register , /login and /profiles , inside of the <div className="container"> . /register/login/profiles ,位于<div className="container"> You would then change your <Switch> to now render two routes for the following route cases;然后,您将更改<Switch>以现在为以下路线情况呈现两条路线;

  1. A <Route/> that renders the Landing component on an exact match of / <Route/>Landing组件呈现在/的完全匹配上
  2. A <Route/> that renders your new WithContainer component on no specific route (ie any path that does not exactly match / )一个<Route/> ,它在没有特定路线(即不完全匹配/任何路径)上呈现您的新WithContainer组件

By using the <Switch> in this way, it causes the routing behaviour to render either Landing or WithContainer (but not both) depending on the first matched route.通过以这种方式使用<Switch> ,它会导致路由行为根据第一个匹配的路由呈现LandingWithContainer (但不是两者)。 We take advantage of that behaviour to restrict rendering of the WithContainer (and in turn, the <div className="container"> element) for "non / " routes.我们利用这种行为来限制“非/ ”路由的WithContainer (以及<div className="container">元素)的呈现。

In code, this approach could be expressed as:在代码中,这种方法可以表示为:

const WithContainer = () => (
    <div className="container">
       { /* Wrap routes in container div */ }
       <Route exact path="/register" component={Register} />
       <Route exact path="/login" component={Login} />
       <Route exact path="/profiles" component={Profiles} />
    </div>)

const Main = () => (
  <main> 
    <Switch>
      <Route exact path='/' component={Landing}/>
      <Route component={ WithContainer } /> {/* No match route */ }
    </Switch>
  </main>
)

Hope that helps!希望有帮助!

With the latest version of React Router, you can provide an array of strings for the path prop so that a specific route renders on multiple matches:使用最新版本的 React Router,您可以为path属性提供一个字符串数组,以便特定路由在多个匹配项上呈现:

<Route path={['/one', '/two', '/three']} component={SomeComponent} />

Here's a link to the relevant documentation: https://reacttraining.com/react-router/web/api/Route/path-string-string .这是相关文档的链接: https : //reacttraining.com/react-router/web/api/Route/path-string-string

If you don't want to create a separate component, you can just do this.如果你不想创建一个单独的组件,你可以这样做。 You need to keep the inner switch as well, if you want to keep the original functionality.如果您想保留原始功能,您还需要保留内部开关。

// parent switch
<Switch>
  <Route exact path="/" component={Landing} />
  // start route wrap!!!
  <Route> 
    <div className="container">
      <Switch>
        <Route exact path="/register" component={Register} />
        <Route exact path="/login" component={Login} />
        <Route exact path="/profiles" component={Profiles} />
      </Switch>
    </div>
  </Route>
 // end route wrap!!!
</Switch>

You should think of Routes as of any other UI components.您应该将 Routes 视为任何其他 UI 组件。 You can nest them arbitrarily.您可以任意嵌套它们。

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

相关问题 React Router 4为所有路由呈现相同的组件 - React router 4 render same component for all routes React Router - 我应该一次渲染所有路由吗? - React Router - Should I render all the routes at once? 所有路由上都没有匹配 404 路由渲染:react-router-dom - No Match 404 route render on all routes: react-router-dom 重定向反应路由器中的所有路由 - Redirect all routes in react router 是否可以在vue-router中锁定除一个以外的所有路由? 它安全吗? 或许我应该使用另一种方法? - Is it Possible to lock all routes except one, in vue-router ? Is it secure? Or maybe I should use another method? 如何删除除一个之外的所有元素? - How to remove all element except one? 使用多个路由的React Router v4,“一台路由器可能只有一个子元素” - React Router v4, “A router may have only one child element” using multiple routes 如何 1). 放一个 div,2)。 渲染一个内部没有路由的组件<routes>在 v6 React 中使用 React Router?</routes> - How to 1). put a div , 2). render a component without Route inside <Routes> with React Router in v6 React? 反应路由器用作的孩子<Routes>元素错误 - react router to be used as the child of <Routes> element error React Router:将所有路由作为数组获取 - React Router: get all routes as array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM