[]previous and next page

useOptionalRouter

The router the calling component renders under, or undefined outside a RouterProvider.

The router the calling component renders under, or undefined outside a RouterProvider. Unlike useRouter it does not throw, so a component that can run with or without a router (a form that enhances itself, a hook that reads per-page state) can ask.

Import#

ts
import { useOptionalRouter } from "@shadow-js/router";

Signature#

ts
function useOptionalRouter(): Router | undefined

Returns#

The router, or undefined.

Examples#

ts
const router = useOptionalRouter();
if (router !== undefined) router.navigate("/");