diff --git a/docs/docs/usage/use-router.mdx b/docs/docs/usage/use-router.mdx index bda6b42c..57f859cf 100644 --- a/docs/docs/usage/use-router.mdx +++ b/docs/docs/usage/use-router.mdx @@ -97,6 +97,25 @@ const onOpenArtist = () => { } ``` +### `canGoBack` + +Follows the exact same API as the React Navigation's navigation object's [canGoBack](https://reactnavigation.org/docs/navigation-prop/#cangoback) function. + +It returns a boolean that tells whether it's possible to navigate back (eg. if the navigation stack has a screen before the current screen) + +```tsx twoslash +import { useRouter } from 'solito/router' +// ---cut--- + +const { canGoBack } = useRouter() + +const onBackPress = () => { + if (canGoBack()) { + // ... + } +} +``` + ### Stack Replace on Native Solito v2 introduces a new `nativeBehavior` option in the `router.replace()` function to let you use a stack replacement action on native navigators: diff --git a/src/app/navigation/use-router.ts b/src/app/navigation/use-router.ts index 2bf3c09b..baf85ffc 100644 --- a/src/app/navigation/use-router.ts +++ b/src/app/navigation/use-router.ts @@ -121,6 +121,13 @@ export function useRouter() { navigation?.goBack() } }, + canGoBack: () => { + if (Platform.OS === 'web') { + return window.history?.length && window.history.length > 2 + } else { + return navigation?.canGoBack() + } + }, parseNextPath, }), [linkTo, navigation] diff --git a/src/router/use-router.ts b/src/router/use-router.ts index 9b579599..e6ef516f 100644 --- a/src/router/use-router.ts +++ b/src/router/use-router.ts @@ -140,6 +140,13 @@ export function useRouter() { navigation?.goBack() } }, + canGoBack: () => { + if (Platform.OS === 'web') { + return window.history?.length && window.history.length > 2 + } else { + return navigation?.canGoBack() + } + }, parseNextPath, }), [linkTo, navigation]