【发布时间】:2020-02-04 20:37:53
【问题描述】:
我尝试为 react 中的数据实现分页。如果我单击任何页面,将打开所需的页面并显示数据。例如,如果我按 page 3 ,则会加载第 3 页上的数据。之后,当我按 5 时,将加载第 5 页上的数据。现在,如果我按下后退按钮,URL 会显示 page 3 ,但安装的组件是 page 5。有人可以帮我吗?
这是我安装的组件:
componentDidMount = async () => {
const { state } = this.props.location;
if (state && state.mesg) {
this.setState({
mesg: this.props.location.state.mesg,
mesgType: this.props.location.state.mesgType
});
const stateCopy = { ...state };
delete stateCopy.mesg;
this.props.history.replace({ state: stateCopy });
}
this.closeMesg(10000);
let pageNo = this.props.location.search.split("=");
await this.fetchCompaniesAPI(pageNo[1] || 1);
this.setState({ isLoading: false });
};
这里我从 url 中提取页码并调用该页面的 API。
谁能帮帮我。
如果您需要更多详细信息,请告知。
我尝试了 window.onpopstate 然后刷新,但没有帮助
我用 componentDidUpdate 试过了,但还是不行
componentDidUpdate= async (prevProps, prevState)=> {
await this.setState({ isLoading: true });
let pageNo = this.props.location.search.split("=");
if (prevState.pageNo !== pageNo[1]) {
await this.fetchCompaniesAPI(pageNo[1] || 1);
this.setState({ isLoading: false });
}
}
【问题讨论】:
-
你从后端获取数据了吗?
标签: reactjs pagination