【发布时间】:2017-04-05 20:18:31
【问题描述】:
我正在尝试使用自定义对话框在使用未保存的数据导航之前要求用户确认。
按照docs我有:
componentDidMount() {
this.props.router.setRouteLeaveHook(
this.props.route,
this.routerWillLeave
)
}
但不是
routerWillLeave(nextLocation) {
if (!this.props.pristine) {
return 'You have unsaved information, are you sure you want to leave this page?'
}
我有
routerWillLeave(nextLocation) {
if (!this.props.pristine) {
this.setState({open: true})
this.forceUpdatate() //necessary or else render won't be called to open dialog
}
我使用的对话框组件来自material-ui,它只需要一个open 布尔值来控制对话框,它还需要一个handleCancel 和handleContinue 方法,但我不知道如何连接它routerWillLeave。
handleCancel 方法很简单,它只是关闭对话框:
handleCancel() {
this.setState({open: false})
};
我已将对话框组件包装在名为 Notification 的组件中
export default class Notification extends React.Component {
render() {
const { open, handleCancel, handleContinue } = this.props
const actions = [
<FlatButton
label="Cancel"
primary={true}
onTouchTap={handleCancel}
/>,
<FlatButton
label="Continue"
primary={true}
onTouchTap={handleContinue}
/>,
];
return (
<div>
<Dialog
actions={actions}
modal={false}
open={open}
>
You have unsaved data. Discard changes?
</Dialog>
</div>
);
}
}
我可以从父组件调用它,我在渲染方法中有这个:
<Notification open={open} handleCancel={this.handleCancel} handleContinue={this.handleContinue}/>
基本上我的问题是如何将其与routerWillLeave 联系起来而不是显示本机浏览器警报?
【问题讨论】:
标签: javascript react-router react-router-redux