admin 管理员组文章数量: 1086019
I use routes to switch between pages. I have App.js where I have my navbar and routes.
These are routes I have problem with:
<Route path='/catalog' exact ponent={() => <Catalog data={data}/>} />
<Route path='/auction' exact ponent={() => <Auction data={data} />} />
Once you click any of them it shows a list of cars (auction and catalog) These ponents have a mon child ponent that receives props (data. Inside this CarItem we have CarDetails ponent that has to render details of each car item. The problem is that CarDetails does not work ( the issue - props that are passed are undefined.
this is CarItem ponent where I link to CarDetails page and create a Route. Or I should put this route on App.js ???
const CarItem = (data) => {
{data.data.data.map(item => {
return(
<>
<img src={imageCar} className='img-feature'/>
<h5 className='bold'>{item.name}</h5>
<h5 className='color-yellow'>$ {item.price}</h5>
<Link className='btn-item auction-btn mr-2' to={`/carDetails/${item.id}`}>Details</Link>
</div>
<Route exact path={`/carDetails/:id`} render={({match}) => (
<CarDetails item={data.find(item => item.id === match.params.id)}/> )}
/>
</>
and this is CarDetails ponent:
const CarDetails = ({item}) => {
console.log(item, ' for car details')
const { id } = props.match.params
return (
<h4 className='text-dark'>{item.name}</h4>
)
}
I use routes to switch between pages. I have App.js where I have my navbar and routes.
These are routes I have problem with:
<Route path='/catalog' exact ponent={() => <Catalog data={data}/>} />
<Route path='/auction' exact ponent={() => <Auction data={data} />} />
Once you click any of them it shows a list of cars (auction and catalog) These ponents have a mon child ponent that receives props (data. Inside this CarItem we have CarDetails ponent that has to render details of each car item. The problem is that CarDetails does not work ( the issue - props that are passed are undefined.
this is CarItem ponent where I link to CarDetails page and create a Route. Or I should put this route on App.js ???
const CarItem = (data) => {
{data.data.data.map(item => {
return(
<>
<img src={imageCar} className='img-feature'/>
<h5 className='bold'>{item.name}</h5>
<h5 className='color-yellow'>$ {item.price}</h5>
<Link className='btn-item auction-btn mr-2' to={`/carDetails/${item.id}`}>Details</Link>
</div>
<Route exact path={`/carDetails/:id`} render={({match}) => (
<CarDetails item={data.find(item => item.id === match.params.id)}/> )}
/>
</>
and this is CarDetails ponent:
const CarDetails = ({item}) => {
console.log(item, ' for car details')
const { id } = props.match.params
return (
<h4 className='text-dark'>{item.name}</h4>
)
}
Share
Improve this question
edited Mar 26, 2021 at 4:36
Linda Paiste
42.4k8 gold badges80 silver badges116 bronze badges
asked Mar 25, 2021 at 8:59
XenaXena
3873 silver badges13 bronze badges
4
- What's the type of item.id? If it's a number you're paring a number with a string (match.params.id) – Filipe Commented Mar 25, 2021 at 9:05
- @Filipe a number – Xena Commented Mar 25, 2021 at 9:11
- <CarDetails item={data.find(item => item.id === +match.params.id)}/> )} Try adding a + before match.params.id to turn into a number – Filipe Commented Mar 25, 2021 at 9:12
- @Filipe does not work ( – Xena Commented Mar 25, 2021 at 9:37
1 Answer
Reset to default 7You should declare all top level routes at one place, including the one for CarDetails
:
<Route path="/catalog" exact ponent={() => <Catalog data={data} />} />
<Route path="/auction" exact ponent={() => <Auction data={data} />} />
<Route
exact
path="/carDetails/:id"
render={({ match }) => (
<CarDetails item={data.find((item) => String(item.id) === String(match.params.id))} />
)}
/>
And use a link like below to redirect to CarDetails
ponents:
<Link className="btn-item auction-btn mr-2" to={`/carDetails/${item.id}`}>
Details
</Link>
本文标签: javascripthow to show details page using react router domStack Overflow
版权声明:本文标题:javascript - how to show details page using react router dom - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744042437a2523505.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论