【问题标题】:How can you show a modal in react when you press the back button browser?当您按下后退按钮浏览器时,如何在反应中显示模态?
【发布时间】:2018-07-19 09:06:12
【问题描述】:

当用户想要离开指定页面时,我需要显示一个模式。 当用户想要从页面转到不同的链接时,我用getUserConfirmation 解决这个问题:

const getUserConfirmation = (message, callback) => {
const history = createBrowserHistory({
     forceRefresh: true
     })

 if (history.location.pathname == "/add/car") {
     store.dispatch(showModal('ConfirmationLeavingAddPageModal', { callback }));
 }
 }

问题是当我按下浏览器上的后退按钮时,它不再起作用了。 接受任何帮助吗?

【问题讨论】:

  • 我不认为你可以在浏览器后退按钮上创建事件,但你需要处理重定向以实现你想要的

标签: reactjs browser-history


【解决方案1】:

对于react-router 2.4.0+

componentDidMount() {
    this.props.router.setRouteLeaveHook(this.props.route, () => {
        if (history.location.pathname == "/add/car") {
            store.dispatch(showModal('ConfirmationLeavingAddPageModal', {
                callback
            }));
        }
    })
}

另外你需要import { withRouter } from 'react-router'export default withRouter(YourComponent)

【讨论】:

    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多