This repository has been archived by the owner on Dec 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
How to specify custom matcher #255
Comments
As an additional note, I believe it might be possible to provide by plugging in to the
|
With the yet unreleased version, I believe code like this should work. The main change comes at the bottom of the code - I use the raw Obviously not a completely ergonomic API, but I'm happy it's possible! import * as UrlPattern from 'url-pattern';
import { createBrowserRouter } from "@jkillian/redux-little-router/lib/environment/browser-router"
import install from "@jkillian/redux-little-router/lib/install"
type RouteCache = {
route: string,
pattern: UrlPattern,
result: Object
};
const eagerMatcher = (routeList: Array<RouteCache>) =>
(incomingUrl: string) => {
// Discard query strings
const pathname = incomingUrl.split('?')[0];
// Find the route that matches the URL
for (let i = 0; i < routeList.length; i++) {
const storedRoute = routeList[i];
const match = storedRoute.pattern.match(pathname);
if (match) {
// Return the matched params and user-defined result object
return {
route: storedRoute.route,
params: match,
result: storedRoute.result
};
}
}
return null;
};
export const createMatcher = (routes: Object) => {
const routeList = Object.keys(routes).sort().reverse().map(route => ({
route,
pattern: new UrlPattern(route, {segmentValueCharset: 'a-zA-Z0-9-_~ %@.'}),
result: routes[route]
}));
return eagerMatcher(routeList);
};
export const createRouter = () => {
return createBrowserRouter((opts) => install({...opts, createMatcher}));
} |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Hi redux-little-router team! One of the goals of the projects, which I appreciated, was:
Unfortunately, I'm having a lot of trouble figuring out how to override the matching behavior. Do you have an example of how I can provide a custom matcher to the router? Thanks!
I dove into the code some, and found that being able to supply this argument would likely do the trick - but I couldn't figure out anyway to supply it:
redux-little-router/src/util/create-installer.js
Line 27 in 81e85af
The text was updated successfully, but these errors were encountered: