[英]What's the proper way to pass url parameters to a react-router's Link component?
So far I've been replacing the url params' names manually when using a link component, for example:到目前为止,我一直在使用链接组件时手动替换 url 参数的名称,例如:
const userIdParam = ':userId'
const routes = {
userDetails: `/users/${ userIdParam }`
}
<Link to={{
pathname: routes.userDetails.replace(userIdParam, user.id)
}}/>
Does react router support passing the params as props into the to
object or something like this?反应路由器是否支持将参数作为道具传递
to
object 或类似的东西? I'm looking for something similar to the following:我正在寻找类似于以下内容的内容:
const routes = {
userDetails: `/users/:userId`
}
<Link to={{
pathname: routes.userDetails,
props: {
userId: user.id
}
}}/>
I've been going through the typings and the docs but can't really find anything.我一直在浏览打字和文档,但真的找不到任何东西。
Yes it does.是的,它确实。 If you want to navigate around then you can just use:
如果你想四处导航,那么你可以使用:
<Link to="/redirect-to-this-url"></Link>
Incase, you want to pass props via the react-router then do this.如果您想通过 react-router 传递道具,请执行此操作。
// Initial Page
<Link to={{
pathname: "redirected-route",
state: {
variable: value // pass the extracted url params here
}
}}
</Link>
The object passed in the state of Link would be accessible here by:在 Link 的 state 中传递的 object 可以通过以下方式访问:
// Redirected component
this.props.location.state // { variable: "value" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.