简体   繁体   English

React-router 4 —共享组件未更新的NavLink

[英]React-router 4 — NavLink with shared component not updating

When using React-Router 4, certain programmatically-generated NavLinks aren't being marked as active consistently. 使用React-Router 4时,某些以编程方式生成的NavLink不会被标记为始终处于活动状态。 I have a sidebar in my react frontend that looks something like this: 我的前端有一个侧边栏,看起来像这样:

<NavLink to="/">Home</NavLink>
<NavLink to="/books">Books</NavLink>
<NavLink to="/poems">Poems</NavLink>
{['hamlet', 'macbeth'].map(play => (
    <NavLink to={`/plays/${play}` key={`play-${play}`}}>{play}</NavLink>
)}}

And my routing looks something like this: 我的路由如下所示:

<Switch>
    <Route path="/" component={HomePageComponent} exact />
    <Route path="/books" component={BooksComponent} />
    <Route path="/poems" component={PoemsComponent} />
    <Route path="/plays/:title" component={PlayComponent} />
    <Route path="/plays/:title/:act" component={ActComponent} />
</Switch>

When I navigate between to one of the the play links, say from /books to /plays/macbeth , the NavLink updates the tab appropriately. 当我了游戏环节之一之间导航,说从/books/plays/macbeth的NavLink适当更新标签。 When I navigate between the plays, say from /plays/macbeth to /plays/hamlet , the NavLink doesn't update and the original tab (in this case, Macbeth) remains active. 当我剧本之间导航时,例如从/plays/macbeth/plays/hamlet ,NavLink不会更新,并且原始选项卡(在这种情况下为Macbeth)仍然处于活动状态。

I suspect the problem is that because the component is not changing, something in the NavLink isActive check isn't firing. 我怀疑问题在于,因为组件未更改,所以NavLink isActive检查中的某些内容无法触发。 This is further confirmed by the fact that if I navigate to a different sub-link -- say, /plays/hamlet/actI , and then to /plays/macbeth , the Macbeth tab is marked as active as expected. 如果我导航到另一个子链接(例如/plays/hamlet/actI ,然后再/plays/hamlet/actI/plays/macbeth ,则可以进一步确认这一点,这是因为Macbeth选项卡被标记为符合预期。 So, is there a way to force the NavLink to update even if the Component hasn't changed? 因此,有没有一种方法可以强制NavLink进行更新,即使该组件没有更改?

Try using the exact prop: 尝试使用exact道具:

<Route exact path="/plays/:title" component={PlayComponent} />

Doc: https://reacttraining.com/react-router/web/api/Route/exact-bool 文件: https//reacttraining.com/react-router/web/api/Route/exact-bool

UPDATE : Since you think it's not re-rendering because it's the same component, also try this: 更新 :由于您认为它是同一组件,因此不会重新渲染,因此请尝试以下方法:

<Route path="/plays/:title" component={(props) => (<PlayComponent {...props}/>) } />

I faced such a problem too. 我也遇到过这样的问题。 And I have tried those two solutions mentioned above, but failed. 而且我尝试了上述两种解决方案,但均失败了。 In my application, there is a <App> component and it does: 在我的应用程序中,有一个<App>组件,它可以:

<BrowserRouter>
    <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/list/:type" component={List} />
    </Switch>
</BrowserRouter>

and <List> renders <Header /> , <Header /> renders <Category /> , <Category /> renders a series of <NavLink /> , which is like <List>呈现<Header /><Header />呈现<Category /><Category />呈现一系列的<NavLink /> ,就像

<NavLink exact to="/">Home</NavLink>
<NavLink exact to="/list/abc">ABC</NavLink>
<NavLink exact to="/list/efg">EFG</NavLink>

If I switch from Home to ABC then the result meets my expectation: classname active will be set on ABC . 如果我从Home切换到ABC,则结果符合我的期望:classname active将在ABC上设置。 However, if I switch from ABC to EFG, things become terrible, only ABC owns classname active . 但是,如果我从ABC切换到EFG,事情将变得很糟糕,只有ABC拥有classname active

This problem troubles me a lot, wish to gain your answers. 这个问题困扰我很多,希望得到您的答案。 Thanks! 谢谢!

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

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