Created
February 9, 2017 14:20
-
-
Save benbarber/7ae5719fccf9f2c1c33d21d592d50bcf to your computer and use it in GitHub Desktop.
Confirm before navigation with React Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
class MyComponent extends React.Component { | |
componentDidMount() { | |
this.props.router.setRouteLeaveHook(this.props.route, this.routerWillLeave) | |
} | |
routerWillLeave(nextLocation) { | |
// return false to prevent a transition w/o prompting the user | |
if(!window.confirm('Are you sure you want to leave?')) { | |
return false | |
} | |
// or return a string to allow the user to decide | |
return nextLocation | |
} | |
// ... | |
} | |
export { MyComponent } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment