我试图在我的React-Relay前端制作无限滚动分页,但没有成功。RelayJS无限滚动
在这一刻,我有这个阵营组件...
class List extends Component {
state = { loading: false };
componentDidMount() {
window.onscroll =() => {
if (!this.state.loading
&& (window.innerHeight + window.scrollY)
>= document.body.offsetHeight) {
this.setState({loading: true},() => {
this.props.relay.setVariables({
count: this.props.relay.variables.count + 5
}, (readyState) => {
if (readyState.done) {
this.setState({ loading: false });
}
});
});
}
}
}
render() {
return (
<div>
{this.props.viewer.products.edges.map(function(product, i) {
return (<Item key={i} product={product.node} />);
})}
</div>
);
}
};
...包裹在接力容器
export default Relay.createContainer(List, {
initialVariables: {
count: 5
},
fragments: {
viewer:() => Relay.QL`
fragment on Viewer {
products(first: $count) {
total
edges {
node {
${Item.getFragment('product')}
}
}
}
}
`,
}
});
这背后的逻辑似乎很容易。如果您达到某个scrollY
的位置,请设置为count
变量新增加的值,该值将扩展您的边缘列表。
但是这个概念会导致一种情况,在开始的时候我会查询数据库的前5条记录,但是最近我一直在滚动一个will查询N + 5条记录。最后,我将查询数据库中的整个表(数千条记录)!这是非常不可接受的。
所以我试图实现一个cursors
,但我不知道如何从连接中获取数据并扩展结果。这种方法返回给我一个“分页”列表。
此外,上述代码给我此错误,当我向下滚动
resolveImmediate.js 39d8:27未捕获不变冲突:performUpdateIfNecessary:意外批号(电流19,待18)
我会非常感谢任何帮助或示例!
这节省了我几个小时的调试:D谢谢! – minheq