【发布时间】:2021-11-18 11:21:24
【问题描述】:
我正在开发一个 react 应用程序,其中我有一个带有滚动条的表格,并且在每次滚动时我都在更新页码并使用更新的页码进行后续 api 调用,但是页码更新速度非常快,以至于超出了限制页码和api返回空数组,导致数据不完整。
这是我的代码:
handleScroll=async ({ scrollTop }) => {
console.log('hey');
if (this.props.masterName && this.props.codeSystem) {
const params = {};
await this.props.setPageNumber(this.props.page_num + 1);
params.code_system_category_id = this.props.masterName;
params.code_systems_id = this.props.codeSystem;
params.page_num = this.props.page_num;
if (this.props.entityName) params.entity_name = this.props.entityName;
if (this.props.status) params.status = this.props.status;
console.log(params);
await this.props.fetchCodeSets(params);
}
}
这是在每次滚动时都会调用的函数,在每次滚动时,我都使用 await 将页码增加 1,并使用 await 以 this.props.fetchCodeSets 进行 api 调用,这样滚动在完成之前不会超过api调用,但滚动不断被调用,并导致上述错误。
这是我的带有滚动条的表格:
<StyledTable
height={250}
width={this.props.width}
headerHeight={headerHeight}
rowHeight={rowHeight}
rowRenderer={this.rowRenderer}
rowCount={this.props.codeSets.length}
rowGetter={({ index }) => this.props.codeSets[index]}
LoadingRow={this.props.LoadingRow}
overscanRowCount={5}
tabIndex={-1}
className='ui very basic small single line striped table'
columnsList={columns}
onScroll={() => this.handleScroll('scroll')}
/>
我正在使用react-virtualized 表,可以在此处找到文档:
https://github.com/bvaughn/react-virtualized/blob/master/docs/Table.md
任何线索都可以提供帮助!
【问题讨论】:
-
setPageNumber是什么?可以分享一下代码吗?如果我们在从 API 获取结果后设置页码会发生什么?就像await this.props.fetchCodeSets(params)声明之后一样。 -
setPageNumber 只会将 redux 状态下的 page_number 增加 1。
-
如果我们在 API 调用之后实现呢?
-
还是一样的行为
-
只是想知道如何控制滚动,等待调用成功后应该触发
标签: javascript reactjs async-await scroll axios