Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

use-browser-history.ts

18 lines564 Bytes

1import { useCanGoBack, useRouter } from "@tanstack/react-router";
2​
3import { useHistorySnapshot } from "@/lib/history-tracker";
4​
5/** Back/forward state for the fake browser, driven by the router's real history. */
6export function useBrowserHistory() {
7 const router = useRouter();
8 const canGoBack = useCanGoBack();
9 const { index, lastIndex } = useHistorySnapshot();
10​
11 return {
12 canGoBack,
13 canGoForward: index < lastIndex,
14 back: () => router.history.back(),
15 forward: () => router.history.forward(),
16 reload: () => router.invalidate(),
17 };