Params
How params flows
router.parseLocation()walkswindow.location.pathnameminusbasePath. It strips locale-code segments and app-path segments, leavingcurrentRoute.router.getRouteByPath(currentRoute)walks the registered route list and runsmatchUrl(route.path, currentRoute, matcher)on each.- The matcher returns
[ok, params]. The matched route is the first hit; its params are placed onrouter.params. <RouterWrapper>passesrouter.paramsas theparamsprop to the page component (and to every middleware viaMiddlewareProps).
router.add("/users/:id", UserPage);
function UserPage({ params, localeCode }: { params: { id: string }; localeCode: string }) { return <p>User {params.id}</p>;}router.params is also readable globally outside the React tree.
URL matcher
Default matcher:
import type { UrlMatcher } from "@mongez/react-router";
const urlPatternMatcher: UrlMatcher = (pattern) => { // handles :name, :name?, :name+, :name* return { regexp, keys: [{ name }] };};Override with path-to-regexp (or anything else):
import { pathToRegexp } from "path-to-regexp";
setRouterConfigurations({ urlMatcher: (pattern) => { const keys: Array<{ name: string }> = []; const regexp = pathToRegexp(pattern, keys); return { regexp, keys }; },});The matcher is called once per route per pattern; results are memoized in a WeakMap keyed by the matcher function reference (so swapping the matcher via setRouterConfigurations({ urlMatcher: ... }) starts with a fresh cache).
Query string
import { queryString } from "@mongez/react-router";
queryString.all();queryString.parse(searchParams: string); // explicit query stringqueryString.get(key: string, defaultValue?: any);queryString.toString(); // current window.location.search.substring(1)queryString.toQueryString(params: object | string);queryString.update(params, reRender?: boolean);Default parser semantics
queryString.parse("page=2&sort=name&tags[]=a&tags[]=b&nested[k]=v");// → { page: 2, sort: "name", tags: ["a", "b"], nested: { k: "v" } }- Numeric-looking values come back as
number(e.g.page=2→page: 2). key[]=v1&key[]=v2becomes{ key: ["v1", "v2"] }. With a single occurrence, it’s still an array ({ key: ["v1"] }).key[sub]=vbecomes{ key: { sub: "v" } }. Nested objects compose:a[b][c]=v→{ a: { b: { c: "v" } } }.
queryString.toQueryString(...)
queryString.toQueryString({ a: 1, b: [2, 3], nested: { x: 1 } });// → "a=1&b[]=2&b[]=3&nested[x]=1"queryString.update(...)
queryString.update({ page: 2, sort: "date" }); // replaceState, no renderqueryString.update({ page: 2 }, /* reRender */ true); // also calls refresh()queryString.update("page=2&sort=date"); // raw string acceptedNote: update replaces the entire query string, not merges. Pass the full object you want represented.
Swap parsers
import qs from "qs";
setRouterConfigurations({ queryString: { objectParser: (search) => qs.parse(search), stringParser: (obj) => qs.stringify(obj), },});Useful when integrating with a library that has its own query-string convention (e.g. PHP-style nesting, comma-separated arrays).