React Router Redux - 在硬转换到另一个域之前提示

时间:2017-05-02 21:37:04

标签: reactjs routing redux react-router react-redux

希望在用户导航到新域之前触发警报或控制台日志。从文档中,setRouteLeaveHookonLeave应该有效。我遵循了这些directions

import React from 'react'
import { withRouter } from 'react-router'


const Page = withRouter(
  React.createClass({

    componentDidMount() {
      this.props.router.setRouteLeaveHook(this.props.route, this.routerWillLeave)
    },

    routerWillLeave(nextLocation) {
      console.log("Moved to different domain");
      return 'Your work is not saved! Are you sure you want to leave?'
    },

    render() {
      return <a href="http://google.com">Redirect</a>
    }
  })
)

export default Page

Page是我应用中的顶级组件。使用react-router v3和redux。

如何在用户离开我们的网站之前触发警报?

1 个答案:

答案 0 :(得分:0)

在导出中添加withRouter HOC包装,如下所示:

export default withRouter(Page);