简体   繁体   English

当用户单击react-router-dom上的后退按钮时,如何更改组件的状态?

[英]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状态的设置?

Edit 编辑

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.

相关问题 如何在 react-router-dom 中清除浏览器后退按钮历史记录 - How to clear browser back button history in react-router-dom 如何根据 react-router-dom 单击的链接更改组件 - How to change a component on the basis of the link clicked by react-router-dom 如何以允许正确使用后退按钮的方式将 react-router-dom 的 setSearchParams 与具有参数的路由结合起来? - How to combine react-router-dom's setSearchParams with route that has a param in a way that allows correct back button usage? React-Router-Dom 改变页面状态 - React-Router-Dom changing states of the page 如何在 react-router-dom v6 中禁用或防止从浏览器返回按钮返回 - How to disable or prevent going back form browser back button in react-router-dom v6 如何覆盖 react-router-dom Link 组件的反向链接? - How can I override react-router-dom Link component back link? 如何更改组件而不在reactjs和react-router-dom中呈现其他组件 - how to change a component without rendering the other component in reactjs and react-router-dom 如何使用 react-router-dom v6 中的按钮路由到另一个反应组件 - How to route to another react component using button in react-router-dom v6 回到react-router-dom时,浏览器历史记录无效 - Browser History not working when going back in react-router-dom 路由更改时 react-router-dom 刷新组件 - react-router-dom refresh component when route changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM