Open History to see the last commit
1import { useRouterState } from './useRouterState'
2import type {
3 StructuralSharingOption,
4 ValidateSelected,
5} from './structuralSharing'
6import type {
7 AnyRouter,
8 RegisteredRouter,
9 RouterState,
10} from '@tanstack/router-core'
11β
12export interface UseLocationBaseOptions<
13 TRouter extends AnyRouter,
14 TSelected,
15 TStructuralSharing extends boolean = boolean,
16> {
17 select?: (
18 state: RouterState<TRouter['routeTree']>['location'],
19 ) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
20}
21β
22export type UseLocationResult<
23 TRouter extends AnyRouter,
24 TSelected,
25> = unknown extends TSelected
26 ? RouterState<TRouter['routeTree']>['location']
27 : TSelected
28β
29/**
30 * Read the current location from the router state with optional selection.
31 * Useful for subscribing to just the pieces of location you care about.
32 *
33 * Options:
34 * - `select`: Project the `location` object to a derived value
35 * - `structuralSharing`: Enable structural sharing for stable references
36 *
37 * @returns The current location (or selected value).
38 * @link https://tanstack.com/router/latest/docs/framework/react/api/router/useLocationHook
39 */
40/**
41 * Read the current location from the router state with optional selection.
42 * Useful for subscribing to just the pieces of location you care about.
43 * @link https://tanstack.com/router/latest/docs/framework/react/api/router/useLocationHook
44 */
45export function useLocation<
46 TRouter extends AnyRouter = RegisteredRouter,
47 TSelected = unknown,
48 TStructuralSharing extends boolean = boolean,
49>(
50 opts?: UseLocationBaseOptions<TRouter, TSelected, TStructuralSharing> &
51 StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
52): UseLocationResult<TRouter, TSelected> {
53 return useRouterState({
54 select: (state: any) =>
55 opts?.select ? opts.select(state.location) : state.location,
56 } as any) as UseLocationResult<TRouter, TSelected>
57}