Skip to content

Commit 1d48262

Browse files
committed
docs(api/hooks): add some extra reference links
1 parent ae97fce commit 1d48262

File tree

2 files changed

+60
-57
lines changed

2 files changed

+60
-57
lines changed

packages/react-router/lib/hooks.tsx

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import {
5353
import type { SerializeFrom } from "./types/route-data";
5454

5555
/**
56-
* Resolves a URL against the current location.
56+
* Resolves a URL against the current {@link Location}.
5757
*
5858
* @example
5959
* import { useHref } from "react-router";
@@ -67,8 +67,8 @@ import type { SerializeFrom } from "./types/route-data";
6767
* @category Hooks
6868
* @param to The path to resolve
6969
* @param options Options
70-
* @param options.relative Defaults to "route" so routing is relative to the route tree.
71-
* Set to "path" to make relative routing operate against path segments.
70+
* @param options.relative Defaults to `"route"` so routing is relative to the route tree.
71+
* Set to `"path"` to make relative routing operate against path segments.
7272
* @returns The resolved href string
7373
*/
7474
export function useHref(
@@ -100,14 +100,14 @@ export function useHref(
100100
}
101101

102102
/**
103-
* Returns true if this component is a descendant of a Router, useful to ensure
103+
* Returns `true` if this component is a descendant of a [`Router`](../declarative-routers/Router), useful to ensure
104104
* a component is used within a Router.
105105
*
106106
* @public
107107
* @category Hooks
108108
* @mode framework
109109
* @mode data
110-
* @returns Whether the component is within a Router context
110+
* @returns Whether the component is within a [`Router`](../declarative-routers/Router) context
111111
*/
112112
export function useInRouterContext(): boolean {
113113
return React.useContext(LocationContext) != null;
@@ -149,26 +149,26 @@ export function useLocation(): Location {
149149
}
150150

151151
/**
152-
* Returns the current navigation action which describes how the router came to
153-
* the current location, either by a pop, push, or replace on the history stack.
152+
* Returns the current {@link Navigation} action which describes how the router came to
153+
* the current {@link Location}, either by a pop, push, or replace on the [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) stack.
154154
*
155155
* @public
156156
* @category Hooks
157-
* @returns The current navigation type (Action.Pop, Action.Push, or Action.Replace)
157+
* @returns The current {@link Navigation} type ({@link Action.Pop}, {@link Action.Push}, or {@link Action.Replace})
158158
*/
159159
export function useNavigationType(): NavigationType {
160160
return React.useContext(LocationContext).navigationType;
161161
}
162162

163163
/**
164-
* Returns a PathMatch object if the given pattern matches the current URL.
164+
* Returns a {@link PathMatch} object if the given pattern matches the current URL.
165165
* This is useful for components that need to know "active" state, e.g.
166-
* `<NavLink>`.
166+
* {@link NavLink | `<NavLink>`}.
167167
*
168168
* @public
169169
* @category Hooks
170-
* @param pattern The pattern to match against the current location
171-
* @returns The path match object if the pattern matches, null otherwise
170+
* @param pattern The pattern to match against the current {@link Location}
171+
* @returns The path match object if the pattern matches, `null` otherwise
172172
*/
173173
export function useMatch<
174174
ParamKey extends ParamParseKey<Path>,
@@ -219,16 +219,16 @@ function useIsomorphicLayoutEffect(
219219
*
220220
* It's often better to use {@link redirect} in [`action`](../../start/framework/route-module#action)/[`loader`](../../start/framework/route-module#loader) functions than this hook.
221221
*
222-
* The returned function signature is `navigate(to, options?)/navigate(delta)` where:
222+
* The returned function signature is `navigate(to, options?)`/`navigate(delta)` where:
223223
*
224-
* * `to` can be a string path, a `To` object, or a number (delta)
224+
* * `to` can be a string path, a {@link To} object, or a number (delta)
225225
* * `options` contains options for modifying the navigation
226-
* * `flushSync`: Wrap the DOM updates in `ReactDom.flushSync`
226+
* * `flushSync`: Wrap the DOM updates in [`ReactDom.flushSync`](https://react.dev/reference/react-dom/flushSync)
227227
* * `preventScrollReset`: Do not scroll back to the top of the page after navigation
228-
* * `relative`: "route" or "path" to control relative routing logic
229-
* * `replace`: Replace the current entry in the history stack
230-
* * `state`: Optional history state to include with the new `Location`
231-
* * `viewTransition`: Enable `document.startViewTransition` for this navigation
228+
* * `relative`: `"route"` or `"path"` to control relative routing logic
229+
* * `replace`: Replace the current entry in the [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) stack
230+
* * `state`: Optional history state to include with the new {@link Location}
231+
* * `viewTransition`: Enable [`document.startViewTransition`](https://developer.mozilla.org/en-US/docs/Web/API/Document/startViewTransition) for this navigation
232232
*
233233
* @example
234234
* import { useNavigate } from "react-router";
@@ -250,7 +250,7 @@ function useIsomorphicLayoutEffect(
250250
* navigate("/some/route?search=param");
251251
* ```
252252
*
253-
* ### Navigate with a `To` object
253+
* ### Navigate with a {@link To} object
254254
*
255255
* All properties are optional.
256256
*
@@ -273,17 +273,17 @@ function useIsomorphicLayoutEffect(
273273
* navigate(-1);
274274
*
275275
* // forward
276-
* // often used in a multi-step wizard workflows
276+
* // often used in a multistep wizard workflows
277277
* navigate(1);
278278
* ```
279279
*
280-
* Be cautions with `navigate(number)`. If your application can load up to a route that has a button that tries to navigate forward/back, there may not be a history entry to go back or forward to, or it can go somewhere you don't expect (like a different domain).
280+
* Be cautious with `navigate(number)`. If your application can load up to a route that has a button that tries to navigate forward/back, there may not be a `[`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) entry to go back or forward to, or it can go somewhere you don't expect (like a different domain).
281281
*
282-
* Only use this if you're sure they will have an entry in the history stack to navigate to.
282+
* Only use this if you're sure they will have an entry in the [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) stack to navigate to.
283283
*
284284
* ### Replace the current entry in the history stack
285285
*
286-
* This will remove the current entry in the history stack, replacing it with a new one, similar to a server side redirect.
286+
* This will remove the current entry in the [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) stack, replacing it with a new one, similar to a server side redirect.
287287
*
288288
* ```tsx
289289
* navigate("/some/route", { replace: true });
@@ -296,13 +296,13 @@ function useIsomorphicLayoutEffect(
296296
* <br/>
297297
* <br/>
298298
*
299-
* To prevent `<ScrollRestoration>` from resetting the scroll position, use the `preventScrollReset` option.
299+
* To prevent {@link ScrollRestoration | `<ScrollRestoration>`} from resetting the scroll position, use the `preventScrollReset` option.
300300
*
301301
* ```tsx
302302
* navigate("?some-tab=1", { preventScrollReset: true });
303303
* ```
304304
*
305-
* For example, if you have a tab interface connected to search params in the middle of a page and you don't want it to scroll to the top when a tab is clicked.
305+
* For example, if you have a tab interface connected to search params in the middle of a page, and you don't want it to scroll to the top when a tab is clicked.
306306
*
307307
* @public
308308
* @category Hooks
@@ -393,20 +393,20 @@ const OutletContext = React.createContext<unknown>(null);
393393
*
394394
* @public
395395
* @category Hooks
396-
* @returns The context value passed to the Outlet
396+
* @returns The context value passed to the {@link Outlet} component
397397
*/
398398
export function useOutletContext<Context = unknown>(): Context {
399399
return React.useContext(OutletContext) as Context;
400400
}
401401

402402
/**
403403
* Returns the element for the child route at this level of the route
404-
* hierarchy. Used internally by `<Outlet>` to render child routes.
404+
* hierarchy. Used internally by {@link Outlet | `<Outlet>`} to render child routes.
405405
*
406406
* @public
407407
* @category Hooks
408408
* @param context The context to pass to the outlet
409-
* @returns The child route element or null if no child routes match
409+
* @returns The child route element or `null` if no child routes match
410410
*/
411411
export function useOutlet(context?: unknown): React.ReactElement | null {
412412
let outlet = React.useContext(RouteContext).outlet;
@@ -419,7 +419,7 @@ export function useOutlet(context?: unknown): React.ReactElement | null {
419419
}
420420

421421
/**
422-
* Returns an object of key/value pairs of the dynamic params from the current URL that were matched by the routes. Child routes inherit all params from their parent routes.
422+
* Returns an object of key/value-pairs of the dynamic params from the current URL that were matched by the routes. Child routes inherit all params from their parent routes.
423423
*
424424
* Assuming a route pattern like `/posts/:postId` is matched by `/posts/123` then `params.postId` will be `"123"`.
425425
*
@@ -530,7 +530,7 @@ export function useParams<
530530
}
531531

532532
/**
533-
* Resolves the pathname of the given `to` value against the current location. Similar to {@link useHref}, but returns a {@link Path} instead of a string.
533+
* Resolves the pathname of the given `to` value against the current {@link Location}. Similar to {@link useHref}, but returns a {@link Path} instead of a string.
534534
*
535535
* @example
536536
* import { useResolvedPath } from "react-router";
@@ -547,9 +547,9 @@ export function useParams<
547547
* @category Hooks
548548
* @param to The path to resolve
549549
* @param options Options
550-
* @param options.relative Defaults to "route" so routing is relative to the route tree.
551-
* Set to "path" to make relative routing operate against path segments.
552-
* @returns The resolved `Path` object with pathname, search, and hash
550+
* @param options.relative Defaults to `"route"` so routing is relative to the route tree.
551+
* Set to `"path"` to make relative routing operate against path segments.
552+
* @returns The resolved {@link Path} object with `pathname`, `search`, and `hash`
553553
*/
554554
export function useResolvedPath(
555555
to: To,
@@ -576,7 +576,6 @@ export function useResolvedPath(
576576
* The return value of `useRoutes` is either a valid React element you can use to render the route tree, or `null` if nothing matched.
577577
*
578578
* @example
579-
* import * as React from "react";
580579
* import { useRoutes } from "react-router";
581580
*
582581
* function App() {
@@ -601,7 +600,7 @@ export function useResolvedPath(
601600
* @public
602601
* @category Hooks
603602
* @param routes An array of route objects that define the route hierarchy
604-
* @param locationArg An optional location object or pathname string to use instead of the current location
603+
* @param locationArg An optional {@link Location} object or pathname string to use instead of the current {@link Location}
605604
* @returns A React element to render the matched route, or `null` if no routes matched
606605
*/
607606
export function useRoutes(
@@ -1172,7 +1171,7 @@ export function useRouteId() {
11721171
}
11731172

11741173
/**
1175-
* Returns the current navigation, defaulting to an "idle" navigation when no navigation is in progress. You can use this to render pending UI (like a global spinner) or read FormData from a form navigation.
1174+
* Returns the current {@link Navigation}, defaulting to an "idle" navigation when no navigation is in progress. You can use this to render pending UI (like a global spinner) or read [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) from a form navigation.
11761175
*
11771176
* @example
11781177
* import { useNavigation } from "react-router";
@@ -1188,17 +1187,18 @@ export function useRouteId() {
11881187
* @category Hooks
11891188
* @mode framework
11901189
* @mode data
1191-
* @returns The current navigation object
1190+
* @returns The current {@link Navigation} object
11921191
*/
11931192
export function useNavigation(): Navigation {
11941193
let state = useDataRouterState(DataRouterStateHook.UseNavigation);
11951194
return state.navigation;
11961195
}
11971196

11981197
/**
1199-
* Revalidate the data on the page for reasons outside of normal data mutations like window focus or polling on an interval.
1198+
* Revalidate the data on the page for reasons outside of normal data mutations like [`Window` focus](https://developer.mozilla.org/en-US/docs/Web/API/Window/focus_event) or polling on an interval.
12001199
*
12011200
* @example
1201+
* ```tsx
12021202
* import { useRevalidator } from "react-router";
12031203
*
12041204
* function WindowFocusRevalidator() {
@@ -1214,8 +1214,9 @@ export function useNavigation(): Navigation {
12141214
* </div>
12151215
* );
12161216
* }
1217+
* ```
12171218
*
1218-
* Note that page data is already revalidated automatically after actions. If you find yourself using this for normal CRUD operations on your data in response to user interactions, you're probably not taking advantage of the other APIs like {@link useFetcher}, {@link Form}, {@link useSubmit} that do this automatically.
1219+
* Note that page data is already revalidated automatically after [`action`](../../start/framework/route-module#action)s. If you find yourself using this for normal CRUD operations on your data in response to user interactions, you're probably not taking advantage of the other APIs like {@link useFetcher}, {@link Form}, {@link useSubmit} that do this automatically.
12191220
*
12201221
* @public
12211222
* @category Hooks
@@ -1240,14 +1241,14 @@ export function useRevalidator(): {
12401241
}
12411242

12421243
/**
1243-
* Returns the active route matches, useful for accessing loaderData for
1244+
* Returns the active route matches, useful for accessing `loaderData` for
12441245
* parent/child routes or the route "handle" property
12451246
*
12461247
* @public
12471248
* @category Hooks
12481249
* @mode framework
12491250
* @mode data
1250-
* @returns An array of UI matches for the current route hierarchy
1251+
* @returns An array of {@link UIMatch | UI matches} for the current route hierarchy
12511252
*/
12521253
export function useMatches(): UIMatch[] {
12531254
let { matches, loaderData } = useDataRouterState(
@@ -1280,7 +1281,7 @@ export function useMatches(): UIMatch[] {
12801281
* @category Hooks
12811282
* @mode framework
12821283
* @mode data
1283-
* @returns The data returned from the route's loader function
1284+
* @returns The data returned from the route's [`loader`](../../start/framework/route-module#loader) or [`clientLoader`](../../start/framework/route-module#clientloader) function
12841285
*/
12851286
export function useLoaderData<T = any>(): SerializeFrom<T> {
12861287
let state = useDataRouterState(DataRouterStateHook.UseLoaderData);
@@ -1289,7 +1290,7 @@ export function useLoaderData<T = any>(): SerializeFrom<T> {
12891290
}
12901291

12911292
/**
1292-
* Returns the loader data for a given route by route ID.
1293+
* Returns the [`loader`](../../start/framework/route-module#loader) data for a given route by route ID.
12931294
*
12941295
* Route IDs are created automatically. They are simply the path of the route file
12951296
* relative to the app folder without the extension.
@@ -1308,15 +1309,15 @@ export function useLoaderData<T = any>(): SerializeFrom<T> {
13081309
* }
13091310
*
13101311
* // You can also specify your own route ID's manually in your routes.ts file:
1311-
* route("/", "containers/app.tsx", { id: "app" }})
1312+
* route("/", "containers/app.tsx", { id: "app" })
13121313
* useRouteLoaderData("app");
13131314
*
13141315
* @public
13151316
* @category Hooks
13161317
* @mode framework
13171318
* @mode data
13181319
* @param routeId The ID of the route to return loader data from
1319-
* @returns The data returned from the specified route's loader function, or undefined if not found
1320+
* @returns The data returned from the specified route's [`loader`](../../start/framework/route-module#loader) function, or `undefined` if not found
13201321
*/
13211322
export function useRouteLoaderData<T = any>(
13221323
routeId: string
@@ -1326,7 +1327,7 @@ export function useRouteLoaderData<T = any>(
13261327
}
13271328

13281329
/**
1329-
* Returns the action data from the most recent POST navigation form submission or `undefined` if there hasn't been one.
1330+
* Returns the [`action`](../../start/framework/route-module#action) data from the most recent `POST` navigation form submission or `undefined` if there hasn't been one.
13301331
*
13311332
* @example
13321333
* import { Form, useActionData } from "react-router";
@@ -1351,7 +1352,7 @@ export function useRouteLoaderData<T = any>(
13511352
* @category Hooks
13521353
* @mode framework
13531354
* @mode data
1354-
* @returns The data returned from the route's action function, or undefined if no action has been called
1355+
* @returns The data returned from the route's [`action`](../../start/framework/route-module#action) function, or `undefined` if no `action` has been called
13551356
*/
13561357
export function useActionData<T = any>(): SerializeFrom<T> | undefined {
13571358
let state = useDataRouterState(DataRouterStateHook.UseActionData);
@@ -1378,7 +1379,7 @@ export function useActionData<T = any>(): SerializeFrom<T> | undefined {
13781379
* @category Hooks
13791380
* @mode framework
13801381
* @mode data
1381-
* @returns The error that was thrown during route loading, action execution, or rendering
1382+
* @returns The error that was thrown during route [loading](../../start/framework/route-module#loader), [`action`](../../start/framework/route-module#action) execution, or rendering
13821383
*/
13831384
export function useRouteError(): unknown {
13841385
let error = React.useContext(RouteErrorContext);
@@ -1413,7 +1414,7 @@ export function useRouteError(): unknown {
14131414
* @category Hooks
14141415
* @mode framework
14151416
* @mode data
1416-
* @returns The resolved value from the nearest Await component
1417+
* @returns The resolved value from the nearest {@link Await} component
14171418
*/
14181419
export function useAsyncValue(): unknown {
14191420
let value = React.useContext(AwaitContext);
@@ -1443,7 +1444,7 @@ export function useAsyncValue(): unknown {
14431444
* @category Hooks
14441445
* @mode framework
14451446
* @mode data
1446-
* @returns The error that was thrown in the nearest Await component
1447+
* @returns The error that was thrown in the nearest {@link Await} component
14471448
*/
14481449
export function useAsyncError(): unknown {
14491450
let value = React.useContext(AwaitContext);
@@ -1454,11 +1455,11 @@ let blockerId = 0;
14541455

14551456
/**
14561457
* Allow the application to block navigations within the SPA and present the
1457-
* user a confirmation dialog to confirm the navigation. Mostly used to avoid
1458-
* using half-filled form data. This does not handle hard-reloads or
1458+
* user a confirmation dialog to confirm the navigation. Mostly used to avoid
1459+
* using half-filled form data. This does not handle hard-reloads or
14591460
* cross-origin navigations.
14601461
*
1461-
* The Blocker object returned by the hook has the following properties:
1462+
* The {@link Blocker} object returned by the hook has the following properties:
14621463
*
14631464
* - **`state`**
14641465
* - `unblocked` - the blocker is idle and has not prevented any navigation
@@ -1554,8 +1555,8 @@ let blockerId = 0;
15541555
* @category Hooks
15551556
* @mode framework
15561557
* @mode data
1557-
* @param shouldBlock Either a boolean or a function returning a boolean which indicates whether the navigation should be blocked. The function format receives a single object parameter containing the `currentLocation`, `nextLocation`, and `historyAction` of the potential navigation.
1558-
* @returns A blocker object with state and reset functionality
1558+
* @param shouldBlock Either a boolean or a function returning a boolean which indicates whether the navigation should be blocked. The function format receives a single object parameter containing the `currentLocation`, `nextLocation`, and `historyAction` of the potential navigation.
1559+
* @returns A {@link Blocker} object with state and reset functionality
15591560
*/
15601561
export function useBlocker(shouldBlock: boolean | BlockerFunction): Blocker {
15611562
let { router, basename } = useDataRouterContext(DataRouterHook.UseBlocker);

scripts/docs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,9 @@ function generateMarkdownForComment(comment: SimplifiedComment): string {
347347
// Example section (if available)
348348
if (comment.example) {
349349
let example = resolveLinkTags(comment.example);
350-
markdown += `\`\`\`tsx\n${example}\n\`\`\`\n\n`;
350+
markdown += example.includes("```")
351+
? `${example}\n\n`
352+
: `\`\`\`tsx\n${example}\n\`\`\`\n\n`;
351353
}
352354

353355
// Signature section

0 commit comments

Comments
 (0)