简体   繁体   English

React Router V6 - useRoutes() 路径正则表达式

[英]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.我知道 react-router-dom v6 处于测试阶段,但我正在使用它并且有几个关于 useRoutes() & path & regex 的问题。

BTW - v6 looks great!!顺便说一句 - v6 看起来很棒!!

Before in v4 I would do the following.在 v4 之前,我会执行以下操作。 It would only match if the path was /profile/<10 digit number>.... (no is profile_id).只有当路径是 /profile/<10 digit number>.... (no is profile_id) 时,它才会匹配。 Is there a way to regex url's like this in v6?有没有办法在 v6 中像这样正则表达式 url?

<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)'.同样在 V4 中,我能够在括号中的参数之后限制参数选项 - ':type(email|forgot)'。 Is there way to do this in v6?有没有办法在 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.它的 profile_id 10 位正则表达式会很棒。 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.我直接写信给 React Router V6 开发团队,目前路径中不支持正则表达式。 There is a small chance it might be added...添加的可能性很小...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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