You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.
Hi redux-little-router team! One of the goals of the projects, which I appreciated, was:
Route matching should be simple and extendable.
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:
As an additional note, I believe it might be possible to provide by plugging in to the createBrowserRouter function? But unfortunately that export isn't available in the latest released version yet:
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 createBrowserRouter function and pass it a custom installer which simply calls the regular installer and supplies it with a custom matcher.
Obviously not a completely ergonomic API, but I'm happy it's possible!
import*asUrlPatternfrom'url-pattern';import{createBrowserRouter}from"@jkillian/redux-little-router/lib/environment/browser-router"importinstallfrom"@jkillian/redux-little-router/lib/install"typeRouteCache={route: string,pattern: UrlPattern,result: Object};consteagerMatcher=(routeList: Array<RouteCache>)=>(incomingUrl: string)=>{// Discard query stringsconstpathname=incomingUrl.split('?')[0];// Find the route that matches the URLfor(leti=0;i<routeList.length;i++){conststoredRoute=routeList[i];constmatch=storedRoute.pattern.match(pathname);if(match){// Return the matched params and user-defined result objectreturn{route: storedRoute.route,params: match,result: storedRoute.result};}}returnnull;};exportconstcreateMatcher=(routes: Object)=>{constrouteList=Object.keys(routes).sort().reverse().map(route=>({
route,pattern: newUrlPattern(route,{segmentValueCharset: 'a-zA-Z0-9-_~ %@.'}),result: routes[route]}));returneagerMatcher(routeList);};exportconstcreateRouter=()=>{returncreateBrowserRouter((opts)=>install({...opts, createMatcher}));}
Sign up for freeto 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: