Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React router activeClassName doesnt set active class for child routes

<ul className="right hide-on-med-and-down">
  <li><IndexLink to="/" activeClassName="active">ABOUT</IndexLink></li>
  <li><Link to="blog" activeClassName="active">BLOG</Link></li>
  <li><Link to="discover" activeClassName="active">DISCOVER</Link></li>
  <li><Link to="contact" activeClassName="active">CONTACT</Link></li>
 </ul>

this is how my navigation looks like, and when i navigate to /blog, Blog is active. but when i navigate to /blog/my-blog-post, the active class disappears, is there a way to make /blog/my-blog-post set the active class on my blog list item?

like image 913
Frederick Mfinanga Avatar asked Oct 30 '22 04:10

Frederick Mfinanga


1 Answers

<Route path="blog" component={Blog}>
  <Route path=":slug" component={Blog}></Route>
</Route>

I just had to declare my routes as above and it worked! This link was helpful: https://github.com/reactjs/react-router/issues/1684

like image 86
Frederick Mfinanga Avatar answered Nov 09 '22 15:11

Frederick Mfinanga