import { FunctionComponent, h } from "preact";
import {
Link,
Params,
Redirect,
Route,
RouteComponentProps,
Router,
Switch,
useLocation,
useRoute,
} from "mwouter/preact";
import useBrowserLocation from "mwouter/use-location";
import staticLocationHook from "mwouter/static-location";
import { MouseEventHandler } from "react";
const Header: FunctionComponent = () =>
;
const Profile = ({ params }: RouteComponentProps<{ id: string }>) => (
User id: {params.id}
);
// example custom hook
type UseNetworkLocation = (options?: {
protocol: string;
address: string;
}) => [string, (to: string, options?: { delay: number }) => void];
/*
* 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
;
;
;
;
This is a mixed content
;
{(params: Params): React.ReactNode => `User id: ${params.id}`}
;
{({ id }) => `User id: ${id}`};
{({ age }) => `User age: ${age}`} // $ExpectError
;
{({ age }: { age: string }) => `User age: ${age}`}
;
; // $ExpectError
// inferred rest params
{(params) => `Rest: ${params.rest}`};
// infer multiple params
{({ first, second, third }) => `${first}, ${second}, ${third}`}
;
// infer only named params
{({ first, second }) => `first: ${first}, second: ${second}`}
;
// for pathToRegexp matcher
{({ user, tab, first, second }) => {
user; // $ExpectType string
tab; // $ExpectType string
first; // $ExpectType string
second; // $ExpectType string | undefined
return `${user}, ${tab}, ${first}, ${second}`;
}}
;
/*
* Link and Redirect component type specs
*/
// `to` and `href` are aliases, but they are mutually exclusive and
// can't be used at the same time:
Users;
About;
; // $ExpectError
; // $ExpectError
About
;
This is awesome!
;
Active Link
;
;
// supports standard link attributes
{}} children={null} />;
;
{
event; // $ExpectType TargetedDragEvent
}}
/>;
/*
* Redirect specs
*/
;
;
;
; // $ExpectError
Redirect({ href: "/home", delay: 1000 });
// example custom hook
type UseLocWithNoOptions = () => [
string,
(to: string, foo: number, bar: string) => void
];
Redirect({ href: "/app" });
something; // $ExpectError
/*
* Switch specs
*/
;
;
/*
* Router specs
*/
; // $ExpectError
const useNetwork: UseNetworkLocation = (() => {}) as UseNetworkLocation;
this is a valid router;
Hello!
;
Hello, we have and some {1337} numbers here.
;
Hello World!;
/*
* Hooks API
*/
const [location, setLocation] = useLocation();
location; // $ExpectType string
// embedded useLocation hook doesn't accept any arguments
const [] = useLocation({}); // $ExpectError
setLocation(); // $ExpectError
setLocation("/app");
setLocation("/app", { replace: true });
setLocation("/app", { unknownOption: true }); // $ExpectError
// custom hook
const [networkLoc, setNetworkLoc] = useLocation();
setNetworkLoc("/home", { delay: 2000 });
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
}
const [, inferedParams] = useRoute("/app/users/:id/:age");
if (inferedParams) {
inferedParams.id; // $ExpectType string
inferedParams.age; // $ExpectType string
} else {
inferedParams; // $ExpectType null
}
/*
* Standalone useLocation hook
*/
const [loc, navigate] = useBrowserLocation();
loc; // $ExpectType string
useBrowserLocation({ base: "/something" });
useBrowserLocation({ foo: "bar" }); // $ExpectError
/*
* staticLocationHook
*/
const myStaticHook = staticLocationHook();
const [staticLoc, staticNavigate] = myStaticHook();
staticLoc; // $ExpectType string
staticNavigate("/something");
staticNavigate("/something", { replace: true });
staticNavigate("/something", { foo: "bar" }); // $ExpectError
myStaticHook.history; // $ExpectType readonly string[]
staticLocationHook("/");
staticLocationHook("/", { record: true });
staticLocationHook("/", { foo: "bar" }); // $ExpectError