简体   繁体   English

Url 当使用路由传递道具时,React Js 中的组件无法接收到参数

[英]Url Parameters not able to recieved in component in react Js when Passing Props with Route

I am passing a props( props of all the details of items from which I will select a product baased on the ID received from URL ) into ItemDetails component and I want to display product according to the item Id which is passed through the URL. unfortunately I am unable to use this.pops.match.params in ItemDetails components I am not understanding why its not reviving the URL parameters can anyone help?我正在将一个道具(我将从 select 中基于从 URL 收到的 ID 的产品的所有细节的道具)传递到 ItemDetails 组件中,我想根据通过 URL 传递的项目 ID 显示产品。不幸的是我无法在 ItemDetails 组件中使用this.pops.match.params我不明白为什么它不恢复 URL 参数谁能帮忙?

This is how I am passing props to the Component.这就是我将道具传递给组件的方式。
<Route exact path='/item/:id' component={()=><ItemDetail itemsData={this.props.itemsData} />} />

Oh I see.我懂了。 One of the issues is that on yow route you have it like: item/:id which I believe :id is dynamic.问题之一是在你的路线上你有这样的东西: item/:id我相信:id是动态的。 Now u see it, if you are not going to receive the same exact value.现在你看到了,如果你不会收到相同的确切值。 The exact should be remove. exact应该被删除。

Other thing that puzzles me is the way you set the component prop, why is it an arrow function and not a reference component={ItemDetail} .另一件让我困惑的事情是你设置component道具的方式,为什么它是一个箭头 function 而不是一个参考component={ItemDetail} If you want to use it as an arrow function the callback gets a parameter call props or any name you want, but it comes the with the props.如果你想将它用作箭头 function 回调获取参数调用props或任何你想要的名称,但它带有道具。 So you need to spread them:所以你需要传播它们:

<Route path=“my-path” component={props=> <ItemDetail {...props} />

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

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