我有一个URL /:id/prices
的路由
但const { id } = useParams()
返回id: undefined
const Component = () => {
const { id } = useParams()
console.log(id)
如何从该路由获取id
?
以下是路由的结构:
<BrowserRouter>
<Routes>
<Route path="/" element={<Listings />} />
<Route path="/:id/prices" element={<PricesHistory />} />
</Routes>
</BrowserRouter>
通过点击下面的按钮,我应该导航到相应的提到的组件
<a href={"/" + id + "/prices"}>
See history →
</a>
1条答案
按热度按时间lkaoscv71#
你需要使用一个Link组件,常规的锚标签不能与react-router一起工作,而且它们不适合单页应用程序,这是react的主要目标: