Skip to content

Commit dd73fdd

Browse files
Add JSDoc comments to React Router components and hooks
Co-authored-by: tannerlinsley <[email protected]>
1 parent d3a3c0b commit dd73fdd

File tree

11 files changed

+99
-10
lines changed

11 files changed

+99
-10
lines changed

packages/react-router/src/Asset.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ interface ScriptAttrs {
88
suppressHydrationWarning?: boolean
99
}
1010

11+
/**
12+
* Render a managed head/body tag from a router-managed descriptor.
13+
* Used internally by `HeadContent` and `Scripts` to materialize tags.
14+
*/
1115
export function Asset({
1216
tag,
1317
attrs,

packages/react-router/src/HeadContent.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { useRouter } from './useRouter'
44
import { useRouterState } from './useRouterState'
55
import type { RouterManagedTag } from '@tanstack/router-core'
66

7+
/**
8+
* Compute the deduped, ordered set of head tags derived from active matches
9+
* and SSR manifest, including title/meta/links/styles/head scripts.
10+
*/
711
export const useTags = () => {
812
const router = useRouter()
913
const nonce = router.options.ssr?.nonce
@@ -187,6 +191,10 @@ export const useTags = () => {
187191
* @description The `HeadContent` component is used to render meta tags, links, and scripts for the current route.
188192
* It should be rendered in the `<head>` of your document.
189193
*/
194+
/**
195+
* Render head tags for the current route tree. Place this in your document
196+
* `<head>` (or as high as possible) to manage title/meta/links/styles.
197+
*/
190198
export function HeadContent() {
191199
const tags = useTags()
192200
const router = useRouter()

packages/react-router/src/ScriptOnce.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { useRouter } from './useRouter'
22

3+
/**
4+
* SSR-only utility component to emit a single inline script once per request.
5+
* Appends an internal callback marker to signal SSR completion.
6+
*/
37
export function ScriptOnce({ children }: { children: string }) {
48
const router = useRouter()
59
if (!router.isServer) {

packages/react-router/src/Scripts.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { useRouterState } from './useRouterState'
33
import { useRouter } from './useRouter'
44
import type { RouterManagedTag } from '@tanstack/router-core'
55

6+
/**
7+
* Render body scripts derived from route `scripts` and SSR manifest assets.
8+
* Place this near the end of your document body.
9+
*/
610
export const Scripts = () => {
711
const router = useRouter()
812
const nonce = router.options.ssr?.nonce

packages/react-router/src/fileRoute.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ import type { UseRouteContextRoute } from './useRouteContext'
5757
* @returns A function that accepts Route options and returns a Route instance.
5858
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createFileRouteFunction
5959
*/
60+
/**
61+
* Creates a file-based Route factory for a given path.
62+
*
63+
* Used by TanStack Router's file-based routing to associate a file with a
64+
* route. The returned function accepts standard route options. In normal usage
65+
* the `path` string is inserted and maintained by the `tsr` generator.
66+
*
67+
* @param path File path literal for the route (usually auto-generated).
68+
* @returns A function that accepts Route options and returns a Route instance.
69+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createFileRouteFunction
70+
*/
6071
export function createFileRoute<
6172
TFilePath extends keyof FileRoutesByPath,
6273
TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'],
@@ -85,6 +96,10 @@ export function createFileRoute<
8596
@deprecated It's no longer recommended to use the `FileRoute` class directly.
8697
Instead, use `createFileRoute('/path/to/file')(options)` to create a file route.
8798
*/
99+
/**
100+
@deprecated It's no longer recommended to use the `FileRoute` class directly.
101+
Instead, use `createFileRoute('/path/to/file')(options)` to create a file route.
102+
*/
88103
export class FileRoute<
89104
TFilePath extends keyof FileRoutesByPath,
90105
TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'],
@@ -184,6 +199,11 @@ export class FileRoute<
184199
Instead, place the loader function in the the main route file, inside the
185200
`createFileRoute('/path/to/file)(options)` options.
186201
*/
202+
/**
203+
@deprecated It's recommended not to split loaders into separate files.
204+
Instead, place the loader function in the the main route file, inside the
205+
`createFileRoute('/path/to/file)(options)` options.
206+
*/
187207
export function FileRouteLoader<
188208
TFilePath extends keyof FileRoutesByPath,
189209
TRoute extends FileRoutesByPath[TFilePath]['preLoaderRoute'],
@@ -308,6 +328,18 @@ export class LazyRoute<TRoute extends AnyRoute> {
308328
* @returns A function that accepts lazy route options and returns a `LazyRoute`.
309329
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createLazyRouteFunction
310330
*/
331+
/**
332+
* Creates a lazily-configurable code-based route stub by ID.
333+
*
334+
* Use this for code-splitting with code-based routes. The returned function
335+
* accepts only non-critical route options like `component`, `pendingComponent`,
336+
* `errorComponent`, and `notFoundComponent` which are applied when the route
337+
* is matched.
338+
*
339+
* @param id Route ID string literal to associate with the lazy route.
340+
* @returns A function that accepts lazy route options and returns a `LazyRoute`.
341+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createLazyRouteFunction
342+
*/
311343
export function createLazyRoute<
312344
TRouter extends AnyRouter = RegisteredRouter,
313345
TId extends string = string,
@@ -343,6 +375,17 @@ export function createLazyRoute<
343375
* @returns A function that accepts lazy route options and returns a `LazyRoute`.
344376
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createLazyFileRouteFunction
345377
*/
378+
/**
379+
* Creates a lazily-configurable file-based route stub by file path.
380+
*
381+
* Use this for code-splitting with file-based routes (eg. `.lazy.tsx` files).
382+
* The returned function accepts only non-critical route options like
383+
* `component`, `pendingComponent`, `errorComponent`, and `notFoundComponent`.
384+
*
385+
* @param id File path literal for the route file.
386+
* @returns A function that accepts lazy route options and returns a `LazyRoute`.
387+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createLazyFileRouteFunction
388+
*/
346389
export function createLazyFileRoute<
347390
TFilePath extends keyof FileRoutesByPath,
348391
TRoute extends FileRoutesByPath[TFilePath]['preLoaderRoute'],

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
/**
615
* Wrap a dynamic import to create a route component that supports
716
* `.preload()` and friendly reload-on-module-missing behavior.

packages/react-router/src/router.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ export class Router<
105105
TRouterHistory,
106106
TDehydrated
107107
> {
108+
/**
109+
* React-specific Router implementation extending the framework-agnostic core.
110+
*
111+
* Use {@link createRouter} to construct instances.
112+
* Pass this instance to {@link RouterProvider} to enable routing.
113+
*/
108114
constructor(
109115
options: RouterConstructorOptions<
110116
TRouteTree,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { useRouterState } from './useRouterState'
22

3+
/**
4+
* Returns whether the router history can safely go back without exiting
5+
* the application. Useful for render-time UI decisions.
6+
*
7+
* @returns boolean indicating back navigation availability.
8+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useCanGoBack
9+
*/
310
export function useCanGoBack() {
411
return useRouterState({ select: (s) => s.location.state.__TSR_index !== 0 })
512
}

packages/react-router/src/useNavigate.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ import type {
77
RegisteredRouter,
88
UseNavigateResult,
99
} from '@tanstack/router-core'
10+
/**
11+
* Programmatic navigation hook.
12+
*
13+
* Returns a stable `navigate` function that updates the current location
14+
* (pathname, search, hash, state) using TanStack Router semantics.
15+
*
16+
* Options:
17+
* - `from`: Base path to resolve relative navigations from (defaults to current route).
18+
*
19+
* @returns A `navigate` function.
20+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useNavigateHook
21+
*/
1022
export function useNavigate<
1123
TRouter extends AnyRouter = RegisteredRouter,
1224
TDefaultFrom extends string = string,

packages/react-router/src/useRouter.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@ import warning from 'tiny-warning'
33
import { getRouterContext } from './routerContext'
44
import type { AnyRouter, RegisteredRouter } from '@tanstack/router-core'
55

6-
/**
7-
* Access the current TanStack Router instance from React context.
8-
* Must be used within a `RouterProvider`.
9-
*
10-
* Options:
11-
* - `warn`: Log a warning if no router context is found (default: true).
12-
*
13-
* @returns The registered router instance.
14-
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useRouterHook
15-
*/
166
/**
177
* Access the current TanStack Router instance from React context.
188
* Must be used within a `RouterProvider`.

0 commit comments

Comments
 (0)