reactjs - React-Router-Dom:我可以调用 `useParams()` 来获取 Route 之外的路径参数吗?
问题描述
我有一个简单的 JSX
<div>
<Header />
<Route exact path = "/:id" component={Home}/>
<Route exact path = "/other/:id" component={Other}/>
</div>
在标题中包含 NavLinks,我希望它们包含以前提供的 ID,例如...
const id = useParams().id;
const root = `/${id}`;
const Other = `/other${root}`;
<NavLink to={root} activeStyle={activeStyle} exact className="bg-link">Home</NavLink>
<NavLink to={other} activeStyle={activeStyle} exact className="bg-link">Other</NavLink>
问题是useParams().id
返回未定义,因为它不在路线本身内。我可以从那里访问这些信息吗?
我正在使用 redux,所以我想知道是否不必将其置于路由器中的 redux 状态。但是,我还不太擅长 redux,所以如果可能的话,我想要一个更直接的答案。
解决方案
推荐阅读
- python - 如何在考勤报告中将 odoo 服务器时区更改为用户时区
- webhooks - 如何更改 MS 团队消息卡的文本颜色
- java - 在请求有效负载中设置字符串列表
- mpi - 定义一个 MPI_type_struct 里面有一个动态数组
- android - 当设备 ARCore 更新到 1.11.0 时,增强图像检测无法正常工作
- sql-server - 尝试循环更新行
- python - 如何在芹菜中正确调用忘记()?
- excel - Excel ActiveX 组合框在条目选择后自行清除
- python - 如果相等,则将数组的值与数据框中的列进行比较,获取另一列中该行的值
- kubernetes - 如何在不更改 kubernetes 中的部署 yaml 的情况下滚动重启 pod?