React-Router-Dom:我可以调用`useParams()`来获取Route之外的路径参数吗?

时间:2020-06-16 13:20:17

标签: reactjs react-router-dom

我有一个简单的JSX

<div>
  <Header />
  <Route exact path = "/:id" component={Home}/>
  <Route exact path = "/other/:id" component={Other}/>
</div>

标题中包含NavLink,我希望它们包括以前提供的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,因此,如果可能,我希望获得更直接的答案。

0 个答案:

没有答案