简体   繁体   English

React-Router 4,动态路径重定向

[英]React-Router 4, Redirect with dynamic path

A user receives an email with invitation code like http://website.com/invitation/%s where %s is an invitation code. 用户收到一封包含邀请代码的电子邮件,例如http://website.com/invitation/%s ,其中%s是邀请代码。

Now I would like to redirect that URL in my app but keep the invitation code like: 现在,我想在我的应用程序中重定向该URL,但保留邀请代码,例如:

<Redirect from="/invitation/:code" to="/auth/register/:code" />

So when the user clicks the link in email: 因此,当用户单击电子邮件中的链接时:

http://website.com/invitation/2abc433

he will be transferred to: 他将被转到:

http://website.com/auth/register/2abc433

Unfortunately, with Redirect component like above, he is transferred to 不幸的是,使用上述重定向组件,他被转移到

http://website.com/auth/register/:code

You can use a stateless component for that : 您可以为此使用无状态组件:

<Route exact path="/invitation/:code" render={props => (
    <Redirect to={`/auth/register/${props.match.params.code}`/>
)}/>

<Redirect> doesn't support a way to pass parameters directly. <Redirect>不支持直接传递参数的方法。

Demo : https://codesandbox.io/s/8kjq4r1m90 演示: https : //codesandbox.io/s/8kjq4r1m90

Make the following line in ES6 在ES6中进行以下一行

<Redirect from="/invitation/:code" to="/auth/register/:code" />

to this 对此

<Redirect from={`/auth/invitation/${code}`} to={`/auth/register/${code}`} />

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

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