@diso.io

groutcho › Other frameworks › React @diso.io/groutcho-react

Reference

context

RouterContext

const RouterContext = createContext<RouterStore | null>(null)

Context carrying the current {@link RouterStore}. Provided by RouterProvider.

hooks

useRouterStore Create a router store that persists across renders.
useRouter The current router store from context.
useMatch Subscribe to the current match.
useGo A stable go function for the context store.
SearchParamsUpdater Read/write the URL query string.
useSearchParams
useOnGo Subscribe an effect to navigation.
useRoute Look up a route by name.
useTitle Imperatively set the current title.
useError Read the error attached to the current match (populated by adapters when a page render throws).

useRouterStore

function useRouterStore(config: RouterStoreConfig): RouterStore

Create a router store that persists across renders. Prefer using RouterProvider; use this directly only when managing the store yourself.

useRouter

function useRouter(): RouterStore

The current router store from context. Throws outside a RouterProvider.

useMatch

function useMatch<P extends Params = Params>(store?: RouterStore): MatchResult & { params: P }

Subscribe to the current match. Re-renders on navigation via useSyncExternalStore. Uses the provided store or the context store.

The P type parameter narrows match.params at the call site — a type-only annotation with no runtime cost. Use it when you know which params the current route defines:

const { slug } = useMatch<{ slug?: string }>().params;

useGo

function useGo(): (input: Input, options?: GoOptions) => MatchResult

A stable go function for the context store.

SearchParamsUpdater

type SearchParamsUpdater =
	| URLSearchParams
	| ((prev: URLSearchParams) => URLSearchParams);

Read/write the URL query string. Read side re-renders on nav (via useMatch); write side rebuilds the URL keeping pathname+hash and calls store.go with the updated query. Pass { replace: true } to overwrite the history entry.

const [params, setParams] = useSearchParams();
const view = params.get("view");
setParams((prev) => { prev.set("view", "grid"); return prev; }, { replace: true });

useSearchParams

function useSearchParams(): [URLSearchParams, (updater: SearchParamsUpdater, options?: GoOptions) => void]

useOnGo

function useOnGo(listener: GoListener): void

Subscribe an effect to navigation. The callback fires with (prev, current) after each successful nav. The callback is captured in a ref, so you can pass an inline function without re-subscribing every render.

useRoute

function useRoute(name: string): Route

Look up a route by name. Routes are static per router — no subscription. Use .href(params) on the returned route to build a URL.

useTitle

function useTitle(title: string): void

Imperatively set the current title. Fires on mount and on title changes. Does not restore on unmount — the next navigation resets the title from route.title.

useError

function useError(): RouteError | undefined

Read the error attached to the current match (populated by adapters when a page render throws). Re-renders on nav via useMatch.

LinkProps
Link An <a> that navigates via the router instead of reloading the page.

LinkProps

interface LinkProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "href">
to?: string A url or route name to navigate to. A /-containing string is treated as a URL path; anything else is treated as a route name paired with params.
params?: Params Params used with a route-name to (and to build the href).
input?: Input A full input object; takes precedence over to/params.
children?: ReactNode
function Link({ to = "", params, input, onClick, children, ...rest }: LinkProps)

An <a> that navigates via the router instead of reloading the page. Modifier/middle clicks fall through to default browser behavior.

NavLinkProps
NavLink A <Link> that adds an activeClass when the current route matches.
interface NavLinkProps extends LinkProps
activeClass?: string Class applied to the <a> when this link's to matches the current route.
activeExact?: boolean Match strictly against the current pathname (path routes) or the route name (name routes). When false (the default) a to that's a path is a prefix match, and a route-name to matches only exact route.
function NavLink({ to, activeClass = "active", activeExact = false, className, children, ...rest }: NavLinkProps): ReactNode

A <Link> that adds an activeClass when the current route matches. Match semantics:

  • to="/foo" (path): active when the current pathname is /foo or a sub-path /foo/.... Set activeExact to require exact equality.
  • to="RouteName": active when the currently matched route has that name.

Provider

ScrollBehavior
RouterRenderArgs
RouterProviderProps
RouterProvider Provides a router store to the tree.
RouterOutlet Standalone matched-page renderer.

ScrollBehavior

type ScrollBehavior =
	| "top"
	| "preserve"
	| "restore"
	| ((prev: MatchResult, current: MatchResult) => void);

RouterRenderArgs

interface RouterRenderArgs {
  match: MatchResult
  store: RouterStore
}

RouterProviderProps

interface RouterProviderProps
  • store?: RouterStore

    An existing store to use. If omitted, one is created from the config below.

  • routes?: Record<string, RouteConfig>

  • redirects?: Record<string, RedirectTest>

  • max_redirects?: number

  • history?: History

    History implementation (defaults to browser when a DOM is present).

  • errorPage?: Component

    Global fallback error UI (used when the matched route has no errorPage).

  • manageTitle?: boolean

    Mirror store.title to document.title. Defaults to true. Set false for SSR or when the host page manages the title itself.

  • scrollBehavior?: ScrollBehavior

    How to reset scroll after each nav. Defaults to "top". Custom function lets you inspect (prev, current) and scroll however you want (e.g. keep scroll for same-section nav).

  • children?: ReactNode | ((args: RouterRenderArgs) => ReactNode)

    Children, or a render-prop called with the current { match, store } that re-renders on navigation.

    When omitted, the provider renders the matched route's page wrapped in its layout chain — the typical "app shell" usage.

RouterProvider

function RouterProvider(props: RouterProviderProps)

Provides a router store to the tree. Supply store, or routes/redirects/ history to have one created (once). Children may be a render-prop, explicit ReactNode, or omitted — in which case the matched route's page (+ layouts,

  • error boundary) is rendered automatically.

RouterOutlet

function RouterOutlet()

Standalone matched-page renderer. Use this if you passed children to RouterProvider (which suppresses auto-rendering) and want to place the matched page somewhere inside your own layout.

From @diso.io/groutcho-core

Exported as they are in @diso.io/groutcho-core: Component, createBrowserHistory, createMemoryHistory, createRouter, ErrorListener, GoListener, GoOptions, History, Input, InputObject, MatchResult, Params, RedirectResult, RedirectTest, Route, RouteConfig, RouteError, RouteInput, Router, RouterConfig, RouteRedirect, RouterStore, RouterStoreConfig, StoreListener, TitleListener, TitleValue.

This page as Markdown