React Router LINK does not pass parameters

我的梦境 提交于 2019-12-06 10:21:14
O95

Since this is a new account I can't comment. I had this same problem too. Are you sure you have spelled it correctly?

since the this.props.params.id variable never grabs the "3277" id number from the URL

Have you tried this.props.match.params.id ?


This is my <Link> in another component

<Link to={`/profile/${this.state.comment.User.id}`}>@{this.state.comment.User.name}</Link>

And my Route

{/* ProfileView */}
<Route path="/profile/:user_id" component={ProfileView} />

and it works well...

I created a similar setup and it worked for me

URL - file:///Users/Arjun/react/static/index.html#/book/1

<Route path="/book/:id" component={BookComponent} />

Can you bundle your js (if you are using webpack or any other framework) and then run the application in incognito/private browsing mode just to ensure that the you have all the latest changes running on the browser. It is likely that browser history/cache is causing the problem.

Can you please try out

 <p><Link to={"/book/" + bookObject.id} params={{ bookObjectId: bookObject.id }}>DISPLAY FULL BOOK DATA</Link></p>

let me know how it went.


Hi, I read your message.

@Peter Hi, it seems like there is an error from other React code that you have. Can you please share the source code where it actually throws an error? Also, it is always a best practice to have "name='pageName' " in your Route tag. If you want to retrieve params from the other page, just use

const {bookObjectId} = this.params;

there is a way you can pass more than one parameter. You can pass "to" as object instead of string.

<Route path="/category/:catId" component={Category} / >

const newTo = { pathname: "/article/595212758daa6810cbba4104", param1: "Par1" };

<Link to={newTo}> </Link>

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