Created
August 22, 2019 19:54
-
-
Save sibelius/49d178731d16a68a8d98495fba4e8a24 to your computer and use it in GitHub Desktop.
hook to give you a safe go back when user access the page directly
This file contains hidden or 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 { useRouter } from 'react-router-dom'; | |
| export const useSafeGoBack = (alternativePath: string) => { | |
| const { history } = useRouter(); | |
| const goBack = () => { | |
| if (history.length > 2) { | |
| history.goBack(); | |
| return; | |
| } | |
| history.push(alternativePath); | |
| }; | |
| return { | |
| goBack, | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment