Open History to see the last commit
1import * as React from 'react'
2import invariant from 'tiny-invariant'
3import { useRouterState } from './useRouterState'
4import { dummyMatchContext, matchContext } from './matchContext'
5import type {
6 StructuralSharingOption,
7 ValidateSelected,
8} from './structuralSharing'
9import type {
10 AnyRouter,
11 MakeRouteMatch,
12 MakeRouteMatchUnion,
13 RegisteredRouter,
14 StrictOrFrom,
15 ThrowConstraint,
16 ThrowOrOptional,
17} from '@tanstack/router-core'
18β
19export interface UseMatchBaseOptions<
20 TRouter extends AnyRouter,
21 TFrom,
22 TStrict extends boolean,
23 TThrow extends boolean,
24 TSelected,
25 TStructuralSharing extends boolean,
26> {
27 select?: (
28 match: MakeRouteMatch<TRouter['routeTree'], TFrom, TStrict>,
29 ) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
30 shouldThrow?: TThrow
31}
32β
33export type UseMatchRoute<out TFrom> = <
34 TRouter extends AnyRouter = RegisteredRouter,
35 TSelected = unknown,
36 TStructuralSharing extends boolean = boolean,
37>(
38 opts?: UseMatchBaseOptions<
39 TRouter,
40 TFrom,
41 true,
42 true,
43 TSelected,
44 TStructuralSharing
45 > &
46 StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
47) => UseMatchResult<TRouter, TFrom, true, TSelected>
48β
49export type UseMatchOptions<
50 TRouter extends AnyRouter,
51 TFrom extends string | undefined,
52 TStrict extends boolean,
53 TThrow extends boolean,
54 TSelected,
55 TStructuralSharing extends boolean,
56> = StrictOrFrom<TRouter, TFrom, TStrict> &
57 UseMatchBaseOptions<
58 TRouter,
59 TFrom,
60 TStrict,
61 TThrow,
62 TSelected,
63 TStructuralSharing
64 > &
65 StructuralSharingOption<TRouter, TSelected, TStructuralSharing>
66β
67export type UseMatchResult<
68 TRouter extends AnyRouter,
69 TFrom,
70 TStrict extends boolean,
71 TSelected,
72> = unknown extends TSelected
73 ? TStrict extends true
74 ? MakeRouteMatch<TRouter['routeTree'], TFrom, TStrict>
75 : MakeRouteMatchUnion<TRouter>
76 : TSelected
77β
78/**
79 * Read and select the nearest or targeted route match.
80 * @link https://tanstack.com/router/latest/docs/framework/react/api/router/useMatchHook
81 */
82export function useMatch<
83 TRouter extends AnyRouter = RegisteredRouter,
84 const TFrom extends string | undefined = undefined,
85 TStrict extends boolean = true,
86 TThrow extends boolean = true,
87 TSelected = unknown,
88 TStructuralSharing extends boolean = boolean,
89>(
90 opts: UseMatchOptions<
91 TRouter,
92 TFrom,
93 TStrict,
94 ThrowConstraint<TStrict, TThrow>,
95 TSelected,
96 TStructuralSharing
97 >,
98): ThrowOrOptional<UseMatchResult<TRouter, TFrom, TStrict, TSelected>, TThrow> {
99 const nearestMatchId = React.useContext(
100 opts.from ? dummyMatchContext : matchContext,
101 )
102β
103 const matchSelection = useRouterState({
104 select: (state: any) => {
105 const match = state.matches.find((d: any) =>
106 opts.from ? opts.from === d.routeId : d.id === nearestMatchId,
107 )
108 invariant(
109 !((opts.shouldThrow ?? true) && !match),
110 `Could not find ${opts.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`,
111 )
112β
113 if (match === undefined) {
114 return undefined
115 }
116β
117 return opts.select ? opts.select(match) : match
118 },
119 structuralSharing: opts.structuralSharing,
120 } as any)
121β
122 return matchSelection as any
123}