[英]How to change component's states when user clicks back button on react-router-dom?
I'm building a small dashboard for a company. 我正在为公司构建一个小的仪表板。 I have a Router in the whole application and in the
/
path I'm showing 5 <Link />
to move through different pages inside the same dashboard. 我在整个应用程序中都有一个路由器,在
/
路径中,我显示5 <Link />
以在同一仪表板内的不同页面之间移动。 The thing I wanted to achieve is that I have to hide these <Link />
tags when entering a different section of the application. 我想要实现的事情是,在进入应用程序的不同部分时,我必须隐藏这些
<Link />
标记。 I'm doing this by changing states and it's working all right. 我正在通过更改状态来做到这一点,并且一切正常。
But the real problem comes when I hit the back button, because the state is not changing. 但是真正的问题出在我按下“后退”按钮时,因为状态没有改变。 Example, when I click Patients the
visibleMenu
state changes to false, to hide the menu. 例如,当我单击“ 患者”时 ,
visibleMenu
状态更改为false,以隐藏菜单。 When I click on dashboard, the visibleMenu
state changes again to true. 当我单击仪表板上时,
visibleMenu
状态再次更改为true。 When I click the back button, the visibleMenu
state stays the same as before clicking the back button. 当我单击后退按钮时,
visibleMenu
状态保持与单击后退按钮之前相同。
Is there a way to handle the back button press with react-router-dom
, so when a user hits back I can check the URL change an set the visibleMenu
state accordingly? 有没有一种方法可以使用
react-router-dom
处理后退按钮,所以当用户回击时,我可以检查URL是否相应地更改了visibleMenu
状态的设置?
I'm adding some code for you to see. 我正在添加一些代码供您查看。
The menu component is this: 菜单组件是这样的:
<div className={this.props.className}>
<div>
<ul className={'dashboardLinks ' + (this.state.visibleMenu ? 'show' : 'hide')}>
<li>
<Link to="/patients" className="br10 sh1 anim-1" onClick={this.hideMenu}>
<span className="fa fa-users"></span>
Patients
</Link>
</li>
<li>
<Link to="/filter" className="br10 sh1 anim-1" onClick={this.hideMenu}>
<span className="fa fa-filter"></span>
Filter Data
</Link>
</li>
<li>
<Link to="/search" className="br10 sh1 anim-1" onClick={this.hideMenu}>
<span className="fa fa-search"></span>
Quick Search
</Link>
</li>
<li>
<Link to="/account" className="br10 sh1 anim-1" onClick={this.hideMenu}>
<span className="fa fa-user"></span>
My Account
</Link>
</li>
<li>
<a href="#logout" className="br10 sh1 anim-1" onClick={this.props.logout}>
<span className="fa fa-sign-out"></span>
Logout
</a>
</li>
</ul>
<Route path="/patients" component={Patients}></Route>
<Route path="/patients/:id" component={PatientInfo}></Route>
<Route path="/filter" component={Filter}></Route>
<Route path="/search" component={Search}></Route>
<Route path="/account" component={Account}></Route>
</div>
</div>
There you can see how I'm showing and hiding the menu depending on a state. 在这里,您可以看到我根据状态显示和隐藏菜单的方式。 That state I need to check if the user reloads the page, to set it according to the URL, if it's only "/" then show the menu, else, don't show it.
这种状态下,我需要检查用户是否重新加载页面,并根据URL进行设置,如果仅是“ /”,则显示菜单,否则不显示。
I'm doing like this: 我是这样的:
componentWillMount() {
var url = window.location.pathname;
if(url === '/') {
this.setState({
visibleMenu: true
});
} else {
this.setState({
visibleMenu: false
});
}
}
But if the user clicks the back button then the state is not updated, so the menu won't show. 但是,如果用户单击“后退”按钮,则状态不会更新,因此菜单不会显示。
由于我没有收到任何答案,所以我只是重新构造了Web应用程序,以声明任何选项的Routes
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.