希望在用户导航到新域之前触发警报或控制台日志。从文档中,setRouteLeaveHook
或onLeave
应该有效。我遵循了这些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。
如何在用户离开我们的网站之前触发警报?
答案 0 :(得分:0)
在导出中添加withRouter
HOC包装,如下所示:
export default withRouter(Page);