Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding <Header /> component on a specific page with react-router

Here's a working code:

const AppRouter = () => (
<BrowserRouter>
  <div>
    <Header />
    <Switch>
      <Route path="/" component={DashboardPage} exact={true} />
      <Route path="/:id" component={ViewerPage} /> // <-- Exclude Header component from this page
      <Route path="/create" component={CreatePage} />
      <Route path="/edit/:id" component={EditPage} />
      <Route path="/help" component={HelpPage} />
      <Route component={NotFoundPage} />
    </Switch>
  </div>
</BrowserRouter>
);

export default AppRouter;

I couldn't seem to figure out how to exclude the Header component from ViewerPage component page. Is there any way to do this?

like image 285
netizen0911 Avatar asked Dec 14 '18 12:12

netizen0911


People also ask

How do I hide the header page in react?

If the raw data for a particular field is not defined, it will be shown as 'Undefined' in the pivot table headers. You can hide those headers by setting the showHeaderWhenEmpty property to false in the pivot table.

What is the BrowserRouter /> component?

BrowserRouter: BrowserRouter is a router implementation that uses the HTML5 history API(pushState, replaceState and the popstate event) to keep your UI in sync with the URL. It is the parent component that is used to store all of the other components.

How hide nav bar in some components react?

To hide navbar in login page in React Router, we can put our nav bar inside the container with the authenticated routes. import React from "react"; const MyComponent = () => { //...

What is the difference between HashRouter and BrowserRouter in react?

HashRouter: When we have small client side applications which doesn't need backend we can use HashRouter because when we use hashes in the URL/location bar browser doesn't make a server request. BrowserRouter: When we have big production-ready applications which serve backend, it is recommended to use <BrowserRouter> .


1 Answers

Had the exact same requirement in my last project and I went with the approach of splitting the header out into its own component and the using React Fragments. Idiomatic!

import React, { Fragment } from 'react'
// ...

const App = () => (
  <div>
    <Switch>
      <Route path='/no-header-route' component={NoHeaderComponent} />   // Route without header
      <Fragment>            // This is the key
        <Header/>           // Header is in its own component
        <Route path='/route-1' component={Comp1}/>  // Route with header
        <Route path='/route-2' component={Comp2}/>  // Route with header
      </Fragment>
    </Switch>
  </div>
)
like image 107
varoons Avatar answered Oct 09 '22 00:10

varoons