Get the path of a certain Route in react-router

折月煮酒 提交于 2021-01-28 12:04:34

问题


Using react-router in with react i have declared some routes like this:

<Route name="voteview" path="voteview/:baselineId" handler={VoteView}/>

In a React Component, i want to call this route. React Router gives me a link:

<Link to="voteview" params={{baselineId: this.props.data.baselineId}}>Voteview</Link>

This results in a <a href...> link.

But i want to use the url path programmatically, not in an <a href...> link. How can i get the URL path to the "voteview" route programmatically?


回答1:


You can use the makeHref method on React Routers context to generate the path like this:

this.context.router.makeHref('voteview', {baselineId: this.props.data.baselineId})



回答2:


As of version version 1.0 of ReactRouter, makeHref(routeName, params, query) is now createHref(pathname, query)

See the v1.0.0 Upgrade Guide



来源:https://stackoverflow.com/questions/32299688/get-the-path-of-a-certain-route-in-react-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!