Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router V6 - useRoutes() Path Regex

I know react-router-dom v6 is in beta but I'm playing with it and have a couple of questions about useRoutes() & path & regex.

BTW - v6 looks great!

Before in v4 I would do the following. It would only match if the path was /profile/<10 digit number>.... (no is profile_id). Is there a way to regex URLs like this in v6?

<Route path="/profile/:userid([0-9]{10})" component={Profile} />

Also in V4 I was able to limit the Param options after the param in brackets - ':type(email|forgot)'. Is there way to do this in v6?

<Route exact path="/login/:type(email|forgot)" component={Login} />

My attempts below were not successful.

{ path: '/profile/:userid([0-9]{10})', element: <Profile /> }

{ path: '/login/:type(email|forgot)', element: <Login /> }

I know with the second one (email|forgot) I can use 'children' and this is fine. I just wanted to ask.

It's the profile_id 10 digit regex that would be great. Would rather not route to the component if that condition isn't met.

like image 232
Adam Avatar asked Feb 23 '26 04:02

Adam


1 Answers

I wrote directly to the React Router V6 Development team and currently regex isn't supported in path. There is a small chance it might be added...

Here is the response - https://github.com/remix-run/react-router/discussions/8132

like image 164
Adam Avatar answered Feb 27 '26 03:02

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!