Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/docs/usage/use-router.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions src/app/navigation/use-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions src/router/use-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ export function useRouter() {
navigation?.goBack()
}
},
canGoBack: () => {
if (Platform.OS === 'web') {
return window.history?.length && window.history.length > 2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return a Boolean, right? It also isn’t SSR safe…

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need to use useSyncExternalStore for web

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I hope you're doing well. I wanted to share my thoughts on this feature. I'm not quite sure how to contribute directly, so I thought I'd leave my suggestion here.

I believe this feature would be great! However, I don’t think we necessarily need to use useSyncExternalStore. A simple check on the browser’s history might be sufficient.

canGoBack: () => {
  if (typeof window !== 'undefined' && Platform.OS === 'web') {
    return window.history?.length > 1;
  } else {
    return navigation?.canGoBack();
  }
},

} else {
return navigation?.canGoBack()
}
},
parseNextPath,
}),
[linkTo, navigation]
Expand Down