import { FunctionComponent, h } from "preact"; import { Link, Params, Redirect, Route, RouteComponentProps, Router, Switch, useLocation, useRoute, } from "mwouter/preact"; const Header: FunctionComponent = () =>
; const Profile = ({ params }: RouteComponentProps<{ id: string }>) => (
User id: {params.id}
); /* * Params type specs */ const someParams: Params = { foo: "bar" }; const someParamsWithGeneric: Params<{ foo: string }> = { foo: "bar" }; // error: params should follow generic type const paramsDontMatchGeneric: Params<{ foo: string }> = { baz: "bar" }; // $ExpectError // error: values are strings! const invalidParams: Params = { id: 13 }; // $ExpectError const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectError /* * component type specs */ // Has `path` prop, should always be a string ; ; // $ExpectError ; // $ExpectError ; // Supports various ways to declare children ; ; path="/profile/:id" component={Profile} />; path="/profile/:name" component={Profile} />; // $ExpectError
; This is a mixed content ; {(params: Params): React.ReactNode => `User id: ${params.id}`} ; path="/users/:id">{({ id }) => `User id: ${id}`}; path="/users/:id"> {({ age }) => `User age: ${age}`} // $ExpectError ; ; // $ExpectError /* * Link and Redirect component type specs */ Users; About; This is awesome! ; ; // $ExpectError ; // $ExpectError Active Link ;
; // supports standard link attributes ; ; ; /* * Redirect specs */ ; ; ; something; // $ExpectError /* * Switch specs */ ; ; /* * Router specs */ ; // $ExpectError Hello! ; Hello, we have
and some {1337} numbers here. ; Hello World!; /* * Hooks API */ const [location, setLocation] = useLocation(); location; // $ExpectType string setLocation(); // $ExpectError setLocation("/app"); setLocation("/app", { replace: true }); setLocation("/app", { unknown: true }); // $ExpectError useLocation({ base: "/app" }); // $ExpectError useRoute(Symbol()); // $ExpectError useRoute(); // $ExpectError useRoute("/"); const [match, params] = useRoute<{ id: string }>("/app/users/:id"); match; // $ExpectType boolean if (params) { params.id; // $ExpectType string params.age; // $ExpectError } else { params; // $ExpectType null } const [, parameters] = useRoute<{ id: string }>("/app/users/:id"); if (parameters) { parameters.id; // $ExpectType string parameters.age; // $ExpectError } else { parameters; // $ExpectType null }