React Router Confirm Navigation
Displays a custom component to confirm or cancel a react-router navigation. Uses react-router's routerWillLeave hook under the hood.
Note: This project is in active development. Breaking API changes may happen without warning until v1.0.
Installation
npm i react-router-confirm-navigation -S
or
yarn add react-router-confirm-navigation
Usage
RouteComponent.js
Can be placed anywhere inside a component that is used for a <Route />
. Uses react router's higher order component withRouter
to pass router props.
;;; { return <div> <ConfirmNavigation allowBackButton> <YourCustomConfirmation onConfirmNavigation= { console; } onCancelNavigation= { console; } allowNavigation= { // condition for allowing or blocking a navigation return false; } /> </ConfirmNavigation> </div> ;};
YourCustomConfirmation.js
Call this.props.confirmNavigation
and this.props.cancelNavigation
anywhere you like from inside the component.
Component // optionally, you can use class methods or pass in as props as shown above // allowNavigation (nextLocation) { return false; } // onConfirmNavigation (currentLocation) {} // onCancelNavigation (nextLocation) {} { return <div> <h4>Warning</h4> <p>Are you sure you wish to leave?</p> <button onClick=thispropsconfirmNavigation>Yep</button> <button onClick=thispropscancelNavigation>Nope</button> </div> ); }}
TODO
- Test usage with react router v4.x.x
- Add more tests for improper component usage