# groutcho › Other frameworks › React › Reference

## context

### `RouterContext`

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

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

## hooks

| | |
| --- | --- |
| [`useRouterStore`](#userouterstore) | Create a router store that persists across renders. |
| [`useRouter`](#userouter) | The current router store from context. |
| [`useMatch`](#usematch) | Subscribe to the current match. |
| [`useGo`](#usego) | A stable `go` function for the context store. |
| [`SearchParamsUpdater`](#searchparamsupdater) | Read/write the URL query string. |
| [`useSearchParams`](#usesearchparams) |  |
| [`useOnGo`](#useongo) | Subscribe an effect to navigation. |
| [`useRoute`](#useroute) | Look up a route by name. |
| [`useTitle`](#usetitle) | Imperatively set the current title. |
| [`useError`](#useerror) | Read the error attached to the current match (populated by adapters when a page render throws). |

### `useRouterStore`

```ts
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`

```ts
function useRouter(): RouterStore
```

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

### `useMatch`

```ts
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:

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

### `useGo`

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

A stable `go` function for the context store.

### `SearchParamsUpdater`

```ts
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.

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

### `useSearchParams`

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

### `useOnGo`

```ts
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`

```ts
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`

```ts
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`

```ts
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`.

## Link

| | |
| --- | --- |
| [`LinkProps`](#linkprops) |  |
| [`Link`](#link-1) | An `<a>` that navigates via the router instead of reloading the page. |

### `LinkProps`

```ts
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` |  |

### `Link`

```ts
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.

## NavLink

| | |
| --- | --- |
| [`NavLinkProps`](#navlinkprops) |  |
| [`NavLink`](#navlink-1) | A `<Link>` that adds an `activeClass` when the current route matches. |

### `NavLinkProps`

```ts
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. |

### `NavLink`

```ts
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`](#scrollbehavior) |  |
| [`RouterRenderArgs`](#routerrenderargs) |  |
| [`RouterProviderProps`](#routerproviderprops) |  |
| [`RouterProvider`](#routerprovider) | Provides a router store to the tree. |
| [`RouterOutlet`](#routeroutlet) | Standalone matched-page renderer. |

### `ScrollBehavior`

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

### `RouterRenderArgs`

```ts
interface RouterRenderArgs {
  match: MatchResult
  store: RouterStore
}
```

### `RouterProviderProps`

```ts
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`

```ts
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`

```ts
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`.
