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 TThrow extends boolean = true,
53>(
54 opts?: UseParamsBaseOptions<
55 TRouter,
56 TFrom,
57 /* TStrict */ true,
58 TThrow,
59 TSelected,
60 TStructuralSharing
61 > &
62 StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
63) => ThrowOrOptional<UseParamsResult<TRouter, TFrom, true, TSelected>, TThrow>
64β
65/**
66 * Access the current route's path parameters with type-safety.
67 *
68 * Options:
69 * - `from`/`strict`: Specify the matched route and whether to enforce strict typing
70 * - `select`: Project the params object to a derived value for memoized renders
71 * - `structuralSharing`: Enable structural sharing for stable references
72 * - `shouldThrow`: Throw if the route is not found in strict contexts
73 *
74 * @returns The params object (or selected value) for the matched route.
75 * @link https://tanstack.com/router/latest/docs/framework/react/api/router/useParamsHook
76 */
77export function useParams<
78 TRouter extends AnyRouter = RegisteredRouter,
79 const TFrom extends string | undefined = undefined,
80 TStrict extends boolean = true,
81 TThrow extends boolean = true,
82 TSelected = unknown,
83 TStructuralSharing extends boolean = boolean,
84>(
85 opts: UseParamsOptions<
86 TRouter,
87 TFrom,
88 TStrict,
89 ThrowConstraint<TStrict, TThrow>,
90 TSelected,
91 TStructuralSharing
92 >,
93): ThrowOrOptional<
94 UseParamsResult<TRouter, TFrom, TStrict, TSelected>,
95 TThrow
96> {
97 return useMatch({
98 from: opts.from!,
99 shouldThrow: opts.shouldThrow,
100 structuralSharing: opts.structuralSharing,
101 strict: opts.strict,
102 select: (match) => {
103 const params = opts.strict === false ? match.params : match._strictParams
104β
105 return opts.select ? opts.select(params) : params
106 },
107 }) as any
108}