Skip to content

Commit d3a3c0b

Browse files
Add JSDoc to public API (#5561)
* Add documentation for React Router hooks and components Co-authored-by: tannerlinsley <[email protected]> * Remove redundant JSDoc comments from react-router Co-authored-by: tannerlinsley <[email protected]> --------- Co-authored-by: Cursor Agent <[email protected]>
1 parent a947885 commit d3a3c0b

File tree

12 files changed

+165
-11
lines changed

12 files changed

+165
-11
lines changed

packages/react-router/src/Match.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ export const MatchInner = React.memo(function MatchInnerImpl({
306306
return out
307307
})
308308

309+
/**
310+
* Render the next child match in the route tree. Typically used inside
311+
* a route component to render nested routes.
312+
*
313+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/outletComponent
314+
*/
309315
export const Outlet = React.memo(function OutletImpl() {
310316
const router = useRouter()
311317
const matchId = React.useContext(matchContext)

packages/react-router/src/Matches.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ declare module '@tanstack/router-core' {
4141
}
4242
}
4343

44+
/**
45+
* Internal component that renders the router's active match tree with
46+
* suspense, error, and not-found boundaries. Rendered by `RouterProvider`.
47+
*/
4448
export function Matches() {
4549
const router = useRouter()
4650
const rootRoute: AnyRoute = router.routesById[rootRouteId]
@@ -169,6 +173,13 @@ export type MakeMatchRouteOptions<
169173
| React.ReactNode
170174
}
171175

176+
/**
177+
* Component that conditionally renders its children based on whether a route
178+
* matches the provided `from`/`to` options. If `children` is a function, it
179+
* receives the matched params object.
180+
*
181+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/matchRouteComponent
182+
*/
172183
export function MatchRoute<
173184
TRouter extends AnyRouter = RegisteredRouter,
174185
const TFrom extends string = string,
@@ -220,6 +231,13 @@ export function useMatches<
220231
} as any) as UseMatchesResult<TRouter, TSelected>
221232
}
222233

234+
/**
235+
* Read the full array of active route matches or select a derived subset.
236+
*
237+
* Useful for debugging, breadcrumbs, or aggregating metadata across matches.
238+
*
239+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useMatchesHook
240+
*/
223241
export function useParentMatches<
224242
TRouter extends AnyRouter = RegisteredRouter,
225243
TSelected = unknown,
@@ -242,6 +260,10 @@ export function useParentMatches<
242260
} as any)
243261
}
244262

263+
/**
264+
* Read the array of active route matches that are children of the current
265+
* match (or selected parent) in the match tree.
266+
*/
245267
export function useChildMatches<
246268
TRouter extends AnyRouter = RegisteredRouter,
247269
TSelected = unknown,

packages/react-router/src/fileRoute.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ import type { UseLoaderDepsRoute } from './useLoaderDeps'
3535
import type { UseLoaderDataRoute } from './useLoaderData'
3636
import type { UseRouteContextRoute } from './useRouteContext'
3737

38+
/**
39+
* Creates a file-based Route factory for a given path.
40+
*
41+
* Used by TanStack Router's file-based routing to associate a file with a
42+
* route. The returned function accepts standard route options. In normal usage
43+
* the `path` string is inserted and maintained by the `tsr` generator.
44+
*
45+
* @param path File path literal for the route (usually auto-generated).
46+
* @returns A function that accepts Route options and returns a Route instance.
47+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createFileRouteFunction
48+
*/
3849
/**
3950
* Creates a file-based Route factory for a given path.
4051
*
@@ -66,6 +77,10 @@ export function createFileRoute<
6677
}).createRoute
6778
}
6879

80+
/**
81+
@deprecated It's no longer recommended to use the `FileRoute` class directly.
82+
Instead, use `createFileRoute('/path/to/file')(options)` to create a file route.
83+
*/
6984
/**
7085
@deprecated It's no longer recommended to use the `FileRoute` class directly.
7186
Instead, use `createFileRoute('/path/to/file')(options)` to create a file route.
@@ -159,6 +174,11 @@ export class FileRoute<
159174
}
160175
}
161176

177+
/**
178+
@deprecated It's recommended not to split loaders into separate files.
179+
Instead, place the loader function in the the main route file, inside the
180+
`createFileRoute('/path/to/file)(options)` options.
181+
*/
162182
/**
163183
@deprecated It's recommended not to split loaders into separate files.
164184
Instead, place the loader function in the the main route file, inside the
@@ -264,6 +284,18 @@ export class LazyRoute<TRoute extends AnyRoute> {
264284
}
265285
}
266286

287+
/**
288+
* Creates a lazily-configurable code-based route stub by ID.
289+
*
290+
* Use this for code-splitting with code-based routes. The returned function
291+
* accepts only non-critical route options like `component`, `pendingComponent`,
292+
* `errorComponent`, and `notFoundComponent` which are applied when the route
293+
* is matched.
294+
*
295+
* @param id Route ID string literal to associate with the lazy route.
296+
* @returns A function that accepts lazy route options and returns a `LazyRoute`.
297+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createLazyRouteFunction
298+
*/
267299
/**
268300
* Creates a lazily-configurable code-based route stub by ID.
269301
*
@@ -289,6 +321,17 @@ export function createLazyRoute<
289321
}
290322
}
291323

324+
/**
325+
* Creates a lazily-configurable file-based route stub by file path.
326+
*
327+
* Use this for code-splitting with file-based routes (eg. `.lazy.tsx` files).
328+
* The returned function accepts only non-critical route options like
329+
* `component`, `pendingComponent`, `errorComponent`, and `notFoundComponent`.
330+
*
331+
* @param id File path literal for the route file.
332+
* @returns A function that accepts lazy route options and returns a `LazyRoute`.
333+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createLazyFileRouteFunction
334+
*/
292335
/**
293336
* Creates a lazily-configurable file-based route stub by file path.
294337
*

packages/react-router/src/lazyRouteComponent.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import * as React from 'react'
22
import { isModuleNotFoundError } from '@tanstack/router-core'
33
import type { AsyncRouteComponent } from './route'
44

5+
/**
6+
* Wrap a dynamic import to create a route component that supports
7+
* `.preload()` and friendly reload-on-module-missing behavior.
8+
*
9+
* @param importer Function returning a module promise
10+
* @param exportName Named export to use (default: `default`)
11+
* @returns A lazy route component compatible with TanStack Router
12+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/lazyRouteComponentFunction
13+
*/
514
export function lazyRouteComponent<
615
T extends Record<string, any>,
716
TKey extends keyof T = 'default',

packages/react-router/src/link.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ import type {
2525
ValidateLinkOptionsArray,
2626
} from './typePrimitives'
2727

28+
/**
29+
* Build anchor-like props for declarative navigation and preloading.
30+
*
31+
* Returns stable `href`, event handlers and accessibility props derived from
32+
* router options and active state. Used internally by `Link` and custom links.
33+
*
34+
* Options cover `to`, `params`, `search`, `hash`, `state`, `preload`,
35+
* `activeProps`, `inactiveProps`, and more.
36+
*
37+
* @returns React anchor props suitable for `<a>` or custom components.
38+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useLinkPropsHook
39+
*/
2840
export function useLinkProps<
2941
TRouter extends AnyRouter = RegisteredRouter,
3042
const TFrom extends string = string,

packages/react-router/src/route.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ declare module '@tanstack/router-core' {
7878
}
7979
}
8080

81+
/**
82+
* Returns a route-specific API that exposes type-safe hooks pre-bound
83+
* to a single route ID. Useful for consuming a route's APIs from files
84+
* where the route object isn't directly imported (e.g. code-split files).
85+
*
86+
* @param id Route ID string literal for the target route.
87+
* @returns A `RouteApi` instance bound to the given route ID.
88+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/getRouteApiFunction
89+
*/
90+
/**
91+
* Returns a route-specific API that exposes type-safe hooks pre-bound
92+
* to a single route ID. Useful for consuming a route's APIs from files
93+
* where the route object isn't directly imported (e.g. code-split files).
94+
*
95+
* @param id Route ID string literal for the target route.
96+
* @returns A `RouteApi` instance bound to the given route ID.
97+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/getRouteApiFunction
98+
*/
8199
/**
82100
* Returns a route-specific API that exposes type-safe hooks pre-bound
83101
* to a single route ID. Useful for consuming a route's APIs from files

packages/react-router/src/useLoaderData.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ export type UseLoaderDataRoute<out TId> = <
5454
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
5555
) => UseLoaderDataResult<TRouter, TId, true, TSelected>
5656

57+
/**
58+
* Read and select the current route's loader data with type‑safety.
59+
*
60+
* Options:
61+
* - `from`/`strict`: Choose which route's data to read and strictness
62+
* - `select`: Map the loader data to a derived value
63+
* - `structuralSharing`: Enable structural sharing for stable references
64+
*
65+
* @returns The loader data (or selected value) for the matched route.
66+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useLoaderDataHook
67+
*/
5768
export function useLoaderData<
5869
TRouter extends AnyRouter = RegisteredRouter,
5970
const TFrom extends string | undefined = undefined,

packages/react-router/src/useLoaderDeps.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ export type UseLoaderDepsRoute<out TId> = <
4040
StructuralSharingOption<TRouter, TSelected, false>,
4141
) => UseLoaderDepsResult<TRouter, TId, TSelected>
4242

43+
/**
44+
* Read and select the current route's loader dependencies object.
45+
*
46+
* Options:
47+
* - `from`: Choose which route's loader deps to read
48+
* - `select`: Map the deps to a derived value
49+
* - `structuralSharing`: Enable structural sharing for stable references
50+
*
51+
* @returns The loader deps (or selected value) for the matched route.
52+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useLoaderDepsHook
53+
*/
4354
export function useLoaderDeps<
4455
TRouter extends AnyRouter = RegisteredRouter,
4556
const TFrom extends string | undefined = undefined,

packages/react-router/src/useLocation.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ export type UseLocationResult<
2626
? RouterState<TRouter['routeTree']>['location']
2727
: TSelected
2828

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+
*/
2940
export function useLocation<
3041
TRouter extends AnyRouter = RegisteredRouter,
3142
TSelected = unknown,

packages/react-router/src/useMatch.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ export type UseMatchResult<
7575
: MakeRouteMatchUnion<TRouter>
7676
: TSelected
7777

78+
/**
79+
* Read and select the nearest or targeted route match.
80+
*
81+
* Options:
82+
* - `from`/`strict`: Target a specific route ID and control union vs. exact typing
83+
* - `select`: Project the match object to a derived value
84+
* - `shouldThrow`: Throw if the match is not found in strict contexts
85+
* - `structuralSharing`: Enable structural sharing for stable references
86+
*
87+
* @returns The active match (or selected value).
88+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useMatchHook
89+
*/
7890
export function useMatch<
7991
TRouter extends AnyRouter = RegisteredRouter,
8092
const TFrom extends string | undefined = undefined,

0 commit comments

Comments
 (0)