-
-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-router): add useCanGoBack (#3017)
also fixes that history does not notify twice for certain actions
- Loading branch information
1 parent
c5d141b
commit 366848e
Showing
13 changed files
with
463 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
id: useCanGoBack | ||
title: useCanGoBack hook | ||
--- | ||
|
||
The `useCanGoBack` hook returns a boolean representing if the router history can safely go back without exiting the application. | ||
|
||
> ⚠️ The following new `useCanGoBack` API is currently _experimental_. | ||
## useCanGoBack returns | ||
|
||
- If the router history is not at index `0`, `true`. | ||
- If the router history is at index `0`, `false`. | ||
|
||
## Limitations | ||
|
||
The router history index is reset after a navigation with [`reloadDocument`](./NavigateOptionsType.md#reloaddocument) set as `true`. This causes the router history to consider the new location as the initial one and will cause `useCanGoBack` to return `false`. | ||
|
||
## Examples | ||
|
||
### Showing a back button | ||
|
||
```tsx | ||
import { useRouter, useCanGoBack } from '@tanstack/react-router' | ||
|
||
function Component() { | ||
const router = useRouter() | ||
const canGoBack = useCanGoBack() | ||
|
||
return ( | ||
<div> | ||
{canGoBack ? ( | ||
<button onClick={() => router.history.back()}>Go back</button> | ||
) : null} | ||
|
||
{/* ... */} | ||
</div> | ||
) | ||
} | ||
``` |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.