Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
React-router v4 - display current page

I have a main component, App that holds all my Match components. I'd like to be able to display the current page at the top (in header, for example) but I'm having trouble getting the current pathname.

In App I have:

App.contextTypes = {
  history: React.PropTypes.object,
};

And I pass the current path name to a component:

<Header currentPathname={this.context.history.location.pathname} />

However, App doesn't seem to update when the location changes. How can I listen for this and pass as a prop?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Presumably you want your header to show on all routes, if you're putting it above your Match components. In that case, also use Match for your header, and match on every pattern and you will have access to location through props.

<Match 
  pattern="/" 
  component={props => <Header pathname={props.location.pathname} />}
/>
over 4 years ago · Santiago Trujillo Report

0

Match is not a component anymore in react-router-dom (^4.3.1). Instead, I did this:

<Route path="" component={(props) => <Header {...props} page={"Main Page"} />}/>

In Header.js:

const Header = (props) => (
  <header className="App-header">
  { console.log(props) }
    <img src={logo} className="App-logo" alt="logo"/>
    <TitleDiv>App Name : {props.page}</TitleDiv>
  </header>
);

The result is App Name : Main Page

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!