【发布时间】:2021-02-20 12:08:24
【问题描述】:
我正在使用 react-router-dom 和 react-connected-router 为一个应用学习 redux 和路由和历史。我有一个 api,我在 componentDidMount() 中执行 fetchData:
componentDidMount() {
const { onFetchData, prodPerPage, currentPage, filters } = this.props;
onFetchBooks(prodPerPage, currentPage, filters);
}
在我的分页组件中,我使用了一个链接,该链接路由到在单击时可以正常工作的页面,因为我在单击时重新获取数据,其中 currentPage 成为 pageIndex,因此为该页面获取数据。
这是我的链接:
<Link to={`/products/page/${item.page}`}>
<PaginationItem {...item} />
</Link>
这是我的路线:
<Route exact path="/products/page/:page" component={ProductsPage} />
如果我重新加载我的页面,我的 currentPage 会从 initialState 设置为 1。 在后退/前进时,我的页面再次加载第 1 页的数据。
如何将我的 currentPage 设置为我导航到的任何内容?因此,如果我的链接是 https://localhost:3000/products/page/3 则将 currentPage 设置为 3 并获取正确的数据?
注意:如果我尝试加载 https://localhost:3000/products/page/3sddada 它不会重定向到 404,我该如何解决这个问题?
提前致谢。
【问题讨论】:
标签: reactjs react-redux react-router-dom