【发布时间】:2020-04-18 23:10:32
【问题描述】:
我在我的项目中使用 react-router-dom 和 webpack。 当用户根据日期和ID点击某个链接时,它会将他们带到另一个页面。
我的路线:
<Switch>
<Route path="/" exact render={Home} />
<Route exact path="/surveys/:date" render={Main} />
<Route path="/surveys/:date/:id" render={Survey} />
<Route render={() => <h1>404: page not found</h1>} />
</Switch>
我希望路径看起来像这样surveys/june/123,它在第一次点击时运行良好。但问题是,当用户从侧边栏加载另一个调查时,url 不会更改为surveys/june/124,而是更改为surveys/june/123/124,之后会更改为surveys/june/123/125,依此类推。例如,第一个加载的 id 始终保留在 url 中。
我试过了:
<Link to={`${match.url}/${id}`} />
和path-to-regexp
const setPath = compile(match.path);
const newPath = setPath({
...match.params,
id: id,
});
它们都不起作用。我做错了什么?
【问题讨论】:
标签: javascript reactjs react-router