Forbes magazine logo Ranked Best Coding Bootcamps 2023

How to get active route name in Navigation layout with React Router

Altcademy Team wrote on 7 February 2018

Hello,
I have a navigation layout with a menu and the title of the current page. How can I get the current route name to show in the title?
Here is how the routing is organized:

<Router history={ browserHistory }> <Route path="/" component={ Layout }> <IndexRoute component={ Index } onEnter={ requireAuth } /> <Route path="/hello" component={ Hello } onEnter={ requireAuth } /> <Route path="/world" component={ World } onEnter={ requireAuth } /> <Route path="/login" component={ Login } /> </Route> </Router>

You should be able to see the current route in this.props.location

Trusted by

Students and instructors from world-class organizations

Imperial College London
Carnegie Mellon University
City University of Hong Kong
Hack Reactor
Cisco Meraki
University of Oxford
Swift
Bazaarvoice
Waterloo
Uber
AtlanTech
Tumblr
Boston College
Bombardier Aerospace
University of St. Andrews
New York University
Minerva Schools at KGI
Merrill Lynch
Riot Games
JP Morgan
Morgan Stanley
Advanced Placement®
Google
KPMG
The University of Hong Kong
University of Toronto
SCMP
Moat
Zynga
Hello Toby
Deloitte
Goldman Sachs
Yahoo
HSBC
General Assembly
Tesla
McGill University
Microsoft

Join the upcoming Cohort #91

Enroll for July 1st, 2024