Forbes magazine logo Ranked Best Coding Bootcamps 2023

React Router Parent Route changes based on conditions of Child Routes

Altcademy Team wrote on 7 February 2018



<Route path="/" component={RouterLayout} extra="foo"> <Route path="login" component={Login} extra="bar"/> </Route>
When I am on /login, I want this.props.route.extra to be "bar" - the props on the "active route", not the parent route.

Currently, you can do this.props.routes[1] - the second level of the "route tree"


<Route path="/"> <Route path="widgets" component={WidgetList} /> <Route path="widgets/:widgetId" component={Widget} foo="bar"/> </Route>

Currently, if I want to read foo="bar", I can't do this.props.route.bar because path="/" is active, instead of path="widgets/:widgetId".
I would have to do this.props.routes[1].bar.
Any suggestions on identifying the actual active path / route? Thanks!

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 #95

Enroll for November 4th, 2024