【发布时间】:2018-12-21 13:41:07
【问题描述】:
我想无限滚动来获取任何项目,简单的步骤是:
1) 在屏幕末尾滚动。
2) 使用 redux 更新项目 pageIndex。
3) 使用 componentDidUpdate 来捕捉这个 redux 动作。
4) 将获取的项目附加到项目数组的末尾。
class InfiniteScroll extends React.Component {
componentDidMount() {
window.addEventListener("scroll", throttle(this.onScroll, 500), false);
}
componentWillUnmount() {
window.removeEventListener("scroll", this.onScroll, false);
}
componentDidUpdate(preProps) {
if (props.pageindex === preProps.pageindex + 1)
fetchItems(...);
}
onScroll = () => {
if (
window.innerHeight + window.scrollY >=
document.body.offsetHeight - 100
) {
updatePage(...);
}
};
render() {
return <Component {...this.props} />;
}
}
此代码的唯一问题是当updatePage 执行一次时,fetchItems 无法立即执行。
【问题讨论】:
-
“
fetchItems无法立即关注”是什么意思?你能展示更多你的代码吗(例如updatePage和fetchItems在做什么)? -
无限滚动来获取东西就像 onPaginated 来显示项目一样,除了你打算在页面末尾滚动而不是点击
next-page按钮。
标签: reactjs redux fetch throttling react-thunk