I am building a Blog app and I am trying to pass id with url for open detail page of blog
so I created Route Link
with id But I have no idea How can I access it from url ? or How can I retrieve id passed in url ?
App.js
class App extends React.Component {
render() {
return (
<div>
...
<Route path='/blog-detail/:id' element={<BlogDetail/>} />
...
</div>
)
}
}
function Blogs() {
return (
<div>
{
blogs.map(blog =>
<div>
{blog.title}
<Link to={`/blog-detail/:${blog.id}`}>Open Blog</Link>
</div>
}
</div>
)
}
// Trying to access id passed in this component
function BlogDetail() {
return (
<div>
Will display blog detail data from Backend by id
</div>
)
}
I have tried many times But it is still not working.
I could easily access by using
window.location.href
andslice
after/
But I think that would not efficient.I can also pass
state={{ id: blog_id }}
usingLink
and access throughuseLocation
But What if user doesn't click fromBlog Component
and input directly from browser.
Any help would be much Appreciated. Thank You in Advance