提交 32e17c5a authored 作者: 王绍森's avatar 王绍森

请求函数增加cancelable

上级 64772150
......@@ -4,6 +4,24 @@ import { ListView, PullToRefresh, } from 'antd-mobile';
import LoadingComponent from './LoadingComponent';
import styles from './ListViewDiy.less';
const makeCancelable = (promise) => {
let canceled = false;
return {
cancel: () => canceled = true,
promise: (...args) => new Promise((resolve, reject) => {
promise(...args).then(res => {
if (!canceled) {
resolve(res);
} else {
reject(res)
}
}).catch(e => {
reject(e);
})
}),
}
};
// 封装的listView组件,展示分页后的数据,可以下拉刷新,滚动到底部请求数据,传递搜索条件刷新。
// props:
......@@ -72,7 +90,9 @@ export class ListViewDiy extends Component {
pageSize,
...searchValues,
};
this.props.request(params).then(res => {
this.cancelableRequest = makeCancelable(this.props.request);
this.cancelableRequest.promise(params).then(res => {
this.setState({ hasSearched: true, })
if (res) {
const { rows, pageNo, pageSize, hasNextPage, } = res;
......@@ -97,6 +117,13 @@ export class ListViewDiy extends Component {
this.setState(({ pageNo }) => ({ pageNo: pageNo + 1}), () => this.queryData(false));
}
componentWillUnmount() {
if (this.cancelableRequest) {
this.cancelableRequest.cancel();
}
}
render() {
const { listViewData, isLoading, isRefreshing, height, hasSearched, dataSource, } = this.state;
const { renderRow, ListSeparator, pageSize = 10, NoResult, renderHeader } = this.props;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论