【问题标题】:how to use redux with routerWillLeave如何将 redux 与 routerWillLeave 一起使用
【发布时间】:2017-01-20 03:41:08
【问题描述】:

所以我有一个 setRouteleaveHook 函数:

routerWillLeave(nextLocation) {
    // return false to prevent a transition w/o prompting the user,
    // or return a string to allow the user to decide:
    console.log("prompt: ", this.props.prompt);
    if (this.props.prompt) {
        return "Are you sure you wish to navigate? Unsaved data will be lost. Try to use the 'next' button instead if moving to the next part.";
    } else {
        return true;
    }
}

this.props.prompt 是存储中的变量。似乎当我在它的父级中更新它时,prompt 直到我下次访问它时才会改变。

有没有更好的方法来触发这些提示? (假设是父组件在做router.push

【问题讨论】:

    标签: javascript reactjs react-router react-redux


    【解决方案1】:

    从'react-router'导入{withRouter};

    export default withRouter(connect(YourSelectors,YourActions)(YourPage))

    【讨论】:

    • this.props.router.setRouterLeaveHook(this.props.route,this.routerWillLeave)
    • 我的问题是没有设置绑定,我已经设置好了。我只是无法为我的孩子正确触发提示。接收存储更改虽然正确。
    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 2022-01-15
    • 1970-01-01
    • 2021-04-14
    • 2021-09-15
    • 2019-01-23
    • 1970-01-01
    相关资源
    最近更新 更多