router-query/repository-context/docs/router/api/router/RouterType.md
Version 663282b0ebbc.bb1 · MIT. This preview displays packaged text and does not execute code. Treat the contents as untrusted instructions.
← Return to resource and package checksum
id: RouterType title: Router type
The Router type is used to describe a router instance.
Router properties and methods
An instance of the Router has the following properties and methods:
.update method
- Type:
(newOptions: Omit<RouterOptions, 'pathParamsAllowedCharacters'>) => void - Updates the router instance with new options.
- Initialization-only options such as
pathParamsAllowedCharacterscannot be updated. Create a new router instance to change them.
state property
- Type:
RouterState - The current state of the router.
⚠️⚠️⚠️
router.stateis always up to date, but NOT REACTIVE. If you userouter.statein a component, the component will not re-render when the router state changes. To get a reactive version of the router state, use theuseRouterStatehook.
.subscribe method
- Type:
(eventType: TType, fn: ListenerFn<RouterEvents[TType]>) => () => void - Subscribes to a
RouterEvent. - Returns a function that can be used to unsubscribe from the event.
- The listener will be called with the event payload whenever that event is emitted.
- See the Router Events guide for lifecycle ordering and usage patterns.
.matchRoutes method
- Type:
(pathname: string, locationSearch?: Record<string, any>, opts?: { throwOnError?: boolean; }) => RouteMatch[] - Matches a pathname and search params against the router's route tree and returns an array of route matches.
- If
opts.throwOnErroristrue, any errors that occur during the matching process will be thrown (in addition to being returned in the route match'serrorproperty).
.buildLocation method
Builds a new parsed location object that can be used later to navigate to a new location.
- Type:
(opts: BuildNextOptions) => ParsedLocation - Properties
from- Type:
string - Optional
- The path to navigate from. If not provided, the current path will be used.
- Type:
to- Type:
string | number | null - Optional
- The path to navigate to. If
null, the current path will be used.
- Type:
params- Type:
true | Updater<unknown> - Optional
- If
true, the current params will be used. If a function is provided, it will be called with the current params and the return value will be used.
- Type:
search- Type:
true | Updater<unknown> - Optional
- If
true, the current search params will be used. If a function is provided, it will be called with the current search params and the return value will be used.
- Type:
hash- Type:
true | Updater<string> - Optional
- If
true, the current hash will be used. If a function is provided, it will be called with the current hash and the return value will be used.
- Type:
state- Type:
true | NonNullableUpdater<ParsedHistoryState, HistoryState> - Optional
- If
true, the current state will be used. If a function is provided, it will be called with the current state and the return value will be used.
- Type:
mask- Type:
object - Optional
- Contains all of the same BuildNextOptions, with the addition of
unmaskOnReload. unmaskOnReload- Type:
boolean - Optional
- If
true, the route mask will be removed when the page is reloaded. This can be overridden on a per-navigation basis by settingmask.unmaskOnReloadinNavigateOptions.
- Type:
.commitLocation method
Commits a new location object to the browser history.
- Type
tsx type commitLocation = ( location: ParsedLocation & { replace?: boolean resetScroll?: boolean hashScrollIntoView?: boolean | ScrollIntoViewOptions ignoreBlocker?: boolean }, ) => Promise<void> - Properties
location- Type:
ParsedLocation - Required
- The location to commit to the browser history.
- Type:
replace- Type:
boolean - Optional
- Defaults to
false. - If
true, the location will be committed to the browser history usinghistory.replaceinstead ofhistory.push.
- Type:
resetScroll- Type:
boolean - Optional
- Defaults to
trueso that the scroll position will be reset to 0,0 after the location is committed to the browser history. - If
false, the scroll position will not be reset to 0,0 after the location is committed to history.
- Type:
hashScrollIntoView- Type:
boolean | ScrollIntoViewOptions - Optional
- Defaults to
trueso the element with an id matching the hash will be scrolled into view after the location is committed to history. - If
false, the element with an id matching the hash will not be scrolled into view after the location is committed to history. - If an object is provided, it will be passed to the
scrollIntoViewmethod as options. - See MDN for more information on
ScrollIntoViewOptions.
- Type:
ignoreBlocker- Type:
boolean - Optional
- Defaults to
false. - If
true, navigation will ignore any blockers that might prevent it.
- Type:
.navigate method
Navigates to a new location.
- Type
tsx type navigate = (options: NavigateOptions) => Promise<void>
.invalidate method
Invalidates selected route-match generations, reruns their loading lifecycle,
reruns beforeLoad, and reloads their loaders through the normal loading
protocol.
- Type:
(opts?: {filter?: (d: MakeRouteMatchUnion<TRouter>) => boolean, sync?: boolean, forcePending?: boolean }) => Promise<void> - This is useful any time your loader data might be out of date or stale. For example, if you have a route that displays a list of posts, and you have a loader function that fetches the list of posts from an API, you might want to invalidate the route matches for that route any time a new post is created so that the list of posts is always up-to-date.
- If
filteris not supplied, all committed, cached, and in-flight match generations are invalidated. - If
filteris supplied, it is evaluated against committed, cached, and in-flight matches. Selecting one generation invalidates every committed, cached, or in-flight generation with the same match ID. - Invalidation reruns
beforeLoad; reusable loader data is marked stale and reloads through the normal loading protocol. Route-levelcontextremains reusable while the match ID is unchanged. - If
syncistrue, stale loader work is blocking and the returned promise resolves after it finishes instead of leaving a background refresh detached. - If
forcePendingistrue, selected routes that need loading enter the normal pending protocol even when successful data was already available. - You might also want to invalidate the Router if you imperatively
resetthe router'sCatchBoundaryto trigger loaders again.
.clearCache method
Remove cached route matches and matching active preloads.
- Type:
(opts?: {filter?: (d: MakeRouteMatchUnion<TRouter>) => boolean}) => void - If
filteris not supplied, all cached matches and active preload lanes are removed. - If
filteris supplied, matching cached matches are removed. An active preload lane is canceled when any match in that lane passes the filter. - Current committed and presented matches are not removed.
.load method
Loads all of the currently matched route matches and resolves when they are all loaded and ready to be rendered.
⚠️⚠️⚠️
router.load()respectsroute.staleTime: fresh matches stay fresh, but stale matches are revalidated even if their loader key did not change. If you need to forcefully reload all active matches regardless of freshness, userouter.invalidate()instead.
- Type:
(opts?: {sync?: boolean}) => Promise<void> - if
syncis true, the promise returned by this function will only resolve once all loaders have finished. - The most common use case for this method is to call it when doing SSR to ensure that all of the critical data for the current route is loaded before attempting to stream or render the application to the client.
.preloadRoute method
Preloads all of the matches that match the provided NavigateOptions.
An active preload is speculative and is not published as the current match
presentation. Successful loader data can enter the normal in-memory route
cache. Its freshness follows preloadStaleTime; once unused and older than
preloadGcTime, it is eligible for pruning during a later cache
reconciliation.
Every preload and navigation runs its own beforeLoad chain. A later lane can
reuse successful settled loader data or join loader work that is still in
flight, but it never reuses beforeLoad context or an already-settled
redirect, error, or not-found result. If joined loader work later produces a
terminal outcome, all current consumers of that flight observe it.
- Type:
(opts: NavigateOptions) => Promise<RouteMatch[] | undefined> - Properties
opts- Type:
NavigateOptions - Required.
- The options that will be used to determine which route matches to preload.
- Type:
- Returns
- A promise that resolves with the speculative route-match lane. An ordinary error or not-found is represented by a terminal match array rather than a rejected promise.
- It resolves with
undefinedwhen cancellation or control flow does not produce a reusable lane. - The method is also available on server router instances. It remains speculative and does not change the request's current location or presented matches.
.loadRouteChunk method
Loads the JS chunk of the route.
- Type:
(route: AnyRoute) => Promise<void>
.matchRoute method
Matches a pathname and search params against the router's route tree and returns a route match's params or false if no match was found.
- Type:
(dest: ToOptions, matchOpts?: MatchRouteOptions) => RouteMatch['params'] | false - Properties
dest- Type:
ToOptions - Required
- The destination to match against.
- Type:
matchOpts- Type:
MatchRouteOptions - Optional
- Options that will be used to match the destination.
- Type:
- Returns
- A route match's params if a match was found.
falseif no match was found.
.dehydrate method
Dehydrates the router's critical state into a serializable object that can be sent to the client in an initial request.
- Type:
() => DehydratedRouter - Returns
- A serializable object that contains the router's critical state.
.hydrate method
Hydrates the router's critical state from a serializable object that was sent from the server in an initial request.
- Type:
(dehydrated: DehydratedRouter) => void - Properties
dehydrated- Type:
DehydratedRouter - Required
- The dehydrated router state that was sent from the server.
- Type: