2017-01-20 41 views
0

所以我有一个setRouteleaveHook与功能:如何使用终极版与routerWillLeave

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的父组件)

回答

0

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

出口默认withRouter(连接(YourSelectors,YourActions)(YourPage))

+0

this.props.router.setRouterLeaveHook(this.props.route,this.routerWillLeave) –

+0

我的问题是没有得到绑定设置,我已经有了。我无法为我的孩子正确触发提示。尽管如此,接收商店更改正确。 –