Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  140] [ 6]  / answers: 1 / hits: 71160  / 7 Years ago, thu, february 2, 2017, 12:00:00

I need a help with react-router v2+
I have to change class of navbar when route changed
for example for route /profile className will be profile-header
I tried to use this.props.location in navbar component but it shows undefined



Hope your help


More From » reactjs

 Answers
61

Your navbar component (as you described it in your question) is probably not the route component, right? By route component I mean the one that you use in your react-router configuration that is loaded for a specific route.



this.props.location is accessible only on such route component, so you need to pass it down to your navbar.



Let's take an example:



Your router config:



<Router>
<Route path=/ component={App}>
// ...
</Router


Route component App:



class App extends React.Component{
// ...
render() {
return <Navbar location={this.props.location}/>
}
}

[#59095] Tuesday, January 31, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;