【发布时间】:2017-01-25 12:43:32
【问题描述】:
我正在尝试在我的 React-Relay 前端进行无限滚动分页,但没有成功。
此刻,我有了这个 React 组件……
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 条记录,但最近我不断滚动查询 N+5 条记录。最后我会在数据库中查询整个表(数千条记录)!这是非常不可接受的。
所以我正在尝试实现cursors,但我不知道如何从连接中获取数据并扩展结果。这种方法返回给我一个 “分页” 列表。
另外,当我向下滚动时,上面的代码给了我这个错误
resolveImmediate.js?39d8:27 Uncaught Invariant Violation: performUpdateIfNecessary: Unexpected batch number (current 19, pending 18)
我将非常感谢任何帮助或示例!
【问题讨论】:
标签: javascript reactjs graphql relayjs