简体   繁体   English

React-Router活动类不适用于嵌套路由

[英]React-Router active class is not working for nested routes

I've looked at several github issues and similar posts, and I can't figure this out. 我看过几个github问题和类似的帖子,我无法弄清楚这一点。 I have here my routes: 我在这里有我的路线:

  <Router history={browserHistory}> <Route path='/' component={Main}> <IndexRoute component={Landing} /> <Route path='login' component={LoginContainer} /> <Route path='user' component={UserContainer} onEnter={checkAuth} > <Route path='home' component={HomeContainer} /> <Route path='settings' component={SettingsContainer} /> <Route path='doc_attributes' component={AttributesContainer} /> <Route path='groups' component={GroupsContainer} /> <Route path='rules' component={RulesContainer} /> </Route> <Route path='/dropbox/auth_finish' onEnter={doDropbox} /> <Route path='/box/auth_finish' onEnter={doBox} /> <Route path='/googledrive/auth_finish' onEnter={doGDrive} /> <Route path='/onedrive/auth_finish' onEnter={doOneDrive} /> </Route> </Router> 

Here are the links of interest: 以下是感兴趣的链接:

 <li><Link to='/user/home' activeClassName="activeLink"><i className="fa fa-home fa-3x fa-fw" aria-hidden="true"></i></Link></li> <li><Link to='/user/settings' activeClassName="activeLink"><i className="fa fa-wrench fa-3x fa-fw" aria-hidden="true"></i></Link></li> <li><Link to='/user/groups' activeClassName="activeLink"><i className="fa fa-users fa-3x fa-fw" aria-hidden="true"></i></Link></li> <li><Link to='/user/rules' activeClassName="activeLink"><i className="fa fa-tasks fa-3x fa-fw" aria-hidden="true"></i></Link></li> 

The links load just fine, but they never get the active class CSS. 链接加载很好,但它们永远不会获得活动类CSS。 Only the first route on load gets it and nothing else. 只有负载上的第一条路线才能获得它而没有别的东西。 Anybody have any ideas? 有人有什么想法吗?

Instead of onEnter you could use componetDidMount 而不是onEnter你可以使用componetDidMount

The bright side here is that you can also use it with componentWillUnmount in order to remove timers for example or to clear cache. 这里的好处是你也可以将它与componentWillUnmount一起使用,以便删除定时器或清除缓存。

I hope this helps, here's some extra info on this subject :) https://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount 我希望这有帮助,这里有一些关于这个主题的额外信息:) https://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount

So, it's been a while since this question was asked, but I figured out the answer so I should share it. 所以,问这个问题已经有一段时间了,但我想出了答案,所以我应该分享一下。

In my container which renders the component and receives the state updates for the navigation, I needed to put {pure: false} in the connect function. 在我的容器中呈现组件并接收导航的状态更新,我需要在连接函数中放置{pure: false} Essentially it has to do with the fact that if your component isn't "pure" (updates only comes from props) then it relies on react context to re-render. 本质上它与如下事实有关:如果你的组件不是“纯粹的”(更新只来自props),那么它依赖于反应上下文来重新渲染。 Here is the block of code: 这是代码块:

const NavContainer = ReactRedux.connect( MapStateToProps, MapDispatchToProps, null, {pure: false} )(Nav)

Documentation for the issue: https://github.com/reactjs/react-redux/blob/master/docs/troubleshooting.md 该问题的文档: https//github.com/reactjs/react-redux/blob/master/docs/troubleshooting.md

The issue which helped me solve it: https://github.com/ReactTraining/react-router/issues/3286 帮助我解决问题的问题: https//github.com/ReactTraining/react-router/issues/3286

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM