Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created August 22, 2019 19:54
Show Gist options
  • Select an option

  • Save sibelius/49d178731d16a68a8d98495fba4e8a24 to your computer and use it in GitHub Desktop.

Select an option

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
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