简体   繁体   中英

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 url's 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 where 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.

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

any info would be good. thankyou very much!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM