0

I want to show the header page always in every route.How to do this. I want props.history to be accessible inside that header component so that if i click any thing i can navigate to other page using props.history.push();

     <BrowserRouter>

    <div>
      <Header/> // I want history in this component
        <Route exact path="/" component={HomePage} />
        <Route  path="/page1" component={Page1} />
        <Route path="/page2" component={Page2} />

    </div>
</BrowserRouter>

);

Trinu
  • 1,133
  • 3
  • 12
  • 34

2 Answers2

3

Use Switch to render one route at a time. If you want history in Header component use withRouter from react-router. (history will be passed as a prop to the header component)

https://reacttraining.com/react-router/web/api/withRouter

/* header component */
import { withRouter } from 'react-router'
export default withRouter(Header)

/* router component */
<BrowserRouter>
    <div>
        <Header/> // I want history in this component
        <Switch>
            <Route exact path="/" component={HomePage} />
            <Route  path="/Picklistscreen" component={PickListArea} />
            <Route path="/scanarea" component={ScanArea} />
        </Switch>
    </div>
</BrowserRouter>
Shubham Gupta
  • 2,328
  • 1
  • 6
  • 18
0

You can create a Layout container which will render your header. If you set path="/" and without exact, it will render thru all routes.

<BrowserRouter>
  <div>
    <Route path="/" component={Layout} />
    <Route exact path="/" component={HomePage} />
    <Route exact path="/Picklistscreen" component={PickListArea} />
    <Route exact path="/scanarea" component={ScanArea} />
  </div>
</BrowserRouter>
froston
  • 746
  • 3
  • 20