Open History to see the last commit
1import { useMatch } from './useMatch'
2import type {
3 StructuralSharingOption,
4 ValidateSelected,
5} from './structuralSharing'
6import type {
7 AnyRouter,
8 RegisteredRouter,
9 ResolveUseParams,
10 StrictOrFrom,
11 ThrowConstraint,
12 ThrowOrOptional,
13 UseParamsResult,
14} from '@tanstack/router-core'
15β
16export interface UseParamsBaseOptions<
17 TRouter extends AnyRouter,
18 TFrom,
19 TStrict extends boolean,
20 TThrow extends boolean,
21 TSelected,
22 TStructuralSharing,
23> {
24 select?: (
25 params: ResolveUseParams<TRouter, TFrom, TStrict>,
26 ) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
27 shouldThrow?: TThrow
28}
29β
30export type UseParamsOptions<
31 TRouter extends AnyRouter,
32 TFrom extends string | undefined,
33 TStrict extends boolean,
34 TThrow extends boolean,
35 TSelected,
36 TStructuralSharing,
37> = StrictOrFrom<TRouter, TFrom, TStrict> &
38 UseParamsBaseOptions<
39 TRouter,
40 TFrom,
41 TStrict,
42 TThrow,
43 TSelected,
44 TStructuralSharing
45 > &
46 StructuralSharingOption<TRouter, TSelected, TStructuralSharing>
47β
48export type UseParamsRoute<out TFrom> = <
49 TRouter extends AnyRouter = RegisteredRouter,
50 TSelected = unknown,
51 TStructuralSharing extends boolean = boolean,
52>(
53 opts?: UseParamsBaseOptions<
54 TRouter,
55 TFrom,
56 /* TStrict */ true,
57 /* TThrow */ true,
58 TSelected,
59 TStructuralSharing
60 > &
61 StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
62) => UseParamsResult<TRouter, TFrom, true, TSelected>
63β
64/**
65 * Access the current route's path parameters with type-safety.
66 *
67 * Options:
68 * - `from`/`strict`: Specify the matched route and whether to enforce strict typing
69 * - `select`: Project the params object to a derived value for memoized renders
70 * - `structuralSharing`: Enable structural sharing for stable references
71 * - `shouldThrow`: Throw if the route is not found in strict contexts
72 *
73 * @returns The params object (or selected value) for the matched route.
74 * @link https://tanstack.com/router/latest/docs/framework/react/api/router/useParamsHook
75 */
76/**
77 * Access the current route's path parameters with type-safety.
78 * @link https://tanstack.com/router/latest/docs/framework/react/api/router/useParamsHook
79 */
80export function useParams<
81 TRouter extends AnyRouter = RegisteredRouter,
82 const TFrom extends string | undefined = undefined,
83 TStrict extends boolean = true,
84 TThrow extends boolean = true,
85 TSelected = unknown,
86 TStructuralSharing extends boolean = boolean,
87>(
88 opts: UseParamsOptions<
89 TRouter,
90 TFrom,
91 TStrict,
92 ThrowConstraint<TStrict, TThrow>,
93 TSelected,
94 TStructuralSharing
95 >,
96): ThrowOrOptional<
97 UseParamsResult<TRouter, TFrom, TStrict, TSelected>,
98 TThrow
99> {
100 return useMatch({
101 from: opts.from!,
102 shouldThrow: opts.shouldThrow,
103 structuralSharing: opts.structuralSharing,
104 strict: opts.strict,
105 select: (match) => {
106 const params = opts.strict === false ? match.params : match._strictParams
107β
108 return opts.select ? opts.select(params) : params
109 },
110 }) as any
111}