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 ResolveUseSearch,
10 StrictOrFrom,
11 ThrowConstraint,
12 ThrowOrOptional,
13 UseSearchResult,
14} from '@tanstack/router-core'
15β
16export interface UseSearchBaseOptions<
17 TRouter extends AnyRouter,
18 TFrom,
19 TStrict extends boolean,
20 TThrow extends boolean,
21 TSelected,
22 TStructuralSharing,
23> {
24 select?: (
25 state: ResolveUseSearch<TRouter, TFrom, TStrict>,
26 ) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
27 shouldThrow?: TThrow
28}
29β
30export type UseSearchOptions<
31 TRouter extends AnyRouter,
32 TFrom,
33 TStrict extends boolean,
34 TThrow extends boolean,
35 TSelected,
36 TStructuralSharing,
37> = StrictOrFrom<TRouter, TFrom, TStrict> &
38 UseSearchBaseOptions<
39 TRouter,
40 TFrom,
41 TStrict,
42 TThrow,
43 TSelected,
44 TStructuralSharing
45 > &
46 StructuralSharingOption<TRouter, TSelected, TStructuralSharing>
47β
48export type UseSearchRoute<out TFrom> = <
49 TRouter extends AnyRouter = RegisteredRouter,
50 TSelected = unknown,
51 TStructuralSharing extends boolean = boolean,
52>(
53 opts?: UseSearchBaseOptions<
54 TRouter,
55 TFrom,
56 /* TStrict */ true,
57 /* TThrow */ true,
58 TSelected,
59 TStructuralSharing
60 > &
61 StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
62) => UseSearchResult<TRouter, TFrom, true, TSelected>
63β
64/**
65 * Read and select the current route's search parameters with type-safety.
66 *
67 * Options:
68 * - `from`/`strict`: Control which route's search is read and how strictly it's typed
69 * - `select`: Map the search object to a derived value for render optimization
70 * - `structuralSharing`: Enable structural sharing for stable references
71 * - `shouldThrow`: Throw when the route is not found (strict contexts)
72 *
73 * @returns The search object (or selected value) for the matched route.
74 * @link https://tanstack.com/router/latest/docs/framework/react/api/router/useSearchHook
75 */
76/**
77 * Read and select the current route's search parameters with type-safety.
78 * @link https://tanstack.com/router/latest/docs/framework/react/api/router/useSearchHook
79 */
80export function useSearch<
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: UseSearchOptions<
89 TRouter,
90 TFrom,
91 TStrict,
92 ThrowConstraint<TStrict, TThrow>,
93 TSelected,
94 TStructuralSharing
95 >,
96): ThrowOrOptional<
97 UseSearchResult<TRouter, TFrom, TStrict, TSelected>,
98 TThrow
99> {
100 return useMatch({
101 from: opts.from!,
102 strict: opts.strict,
103 shouldThrow: opts.shouldThrow,
104 structuralSharing: opts.structuralSharing,
105 select: (match: any) => {
106 return opts.select ? opts.select(match.search) : match.search
107 },
108 }) as any
109}