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
I’d like to request the addition of a matchRoutes function to Wouter. This function is available in react-router, and implementing it in Wouter should require minimal code.
Use Case
The matchRoutes function would allow users to find the active route based on the current location. This is particularly useful for handling layouts or route-based logic efficiently.
Example usage:
constroutes=[{path: "/foo",component: <div>Foo</div>},{path: "/bar",component: <div>Bar</div>,layout: "red"},];constlayouts={default: ({ children })=><div>{children}</div>,red: ({ children })=><divstyle={{backgroundColor: "red"}}>{children}</div>,};exportdefaultfunctionApp(){const[location]=useLocation();constactiveRoute=matchRoutes(routes,location);constLayout=activeRoute?.layout ? layouts[activeRoute.layout] : layouts.default;return(<Layout><Routes>{routes.map((route)=>(<Routekey={route.path}path={route.path}element={route.component}/>))}</Routes></Layout>);}
Example Implementation
This is the implementation I currently use in my project.
(~160 bytes, unminified)
Having this built into the library would prevent redundant loops (like in my example implementation) when matching routes, as Wouter itself already performs route matching internally.
Would love to hear your thoughts on this! Thanks for the awesome work on Wouter! 🚀
The text was updated successfully, but these errors were encountered:
I’d like to request the addition of a
matchRoutes
function to Wouter. This function is available in react-router, and implementing it in Wouter should require minimal code.Use Case
The
matchRoutes
function would allow users to find the active route based on the current location. This is particularly useful for handling layouts or route-based logic efficiently.Example usage:
Example Implementation
This is the implementation I currently use in my project.
(~160 bytes, unminified)
Having this built into the library would prevent redundant loops (like in my example implementation) when matching routes, as Wouter itself already performs route matching internally.
Would love to hear your thoughts on this! Thanks for the awesome work on Wouter! 🚀
The text was updated successfully, but these errors were encountered: