【发布时间】:2020-10-14 21:53:21
【问题描述】:
我正在尝试使用 React Apollo (https://www.apollographql.com/docs/react/data/pagination/#cursor-based) 遵循基于光标的分页示例,但我正在努力解决渲染原始数据的组件如何获取新的(附加的)数据。
这是我们获取原始数据并将其传递给组件的方式:
const { data: { comments, cursor }, loading, fetchMore } = useQuery(
MORE_COMMENTS_QUERY
);
<Comments
entries={comments || []}
onLoadMore={...}
/>
我不确定fetchMore 函数是如何工作的。
onLoadMore={() =>
fetchMore({
query: MORE_COMMENTS_QUERY,
variables: { cursor: cursor },
updateQuery: (previousResult, { fetchMoreResult }) => {
const previousEntry = previousResult.entry;
const newComments = fetchMoreResult.moreComments.comments;
const newCursor = fetchMoreResult.moreComments.cursor;
return {
// By returning `cursor` here, we update the `fetchMore` function
// to the new cursor.
cursor: newCursor,
entry: {
// Put the new comments in the front of the list
comments: [...newComments, ...previousEntry.comments]
},
__typename: previousEntry.__typename
};
}
})
}
据我了解,是的,一旦我的组件调用此 onLoadMore 函数(例如使用按钮的 onClick),它将根据新光标获取数据。
我的问题是这样的。如果这太简单了,我很抱歉,我不懂一些基本的东西。
组件如何获取新数据?
我知道数据在那里,因为我在控制台记录了newComments(在我的情况下,它不是新评论,但你明白了。)我看到了新数据!但是那些新的 cmets,它们是如何返回到需要数据的组件的呢?如果我再次单击该按钮,它仍然停留在与以前相同的光标上。
我在这里错过了什么?
【问题讨论】:
标签: javascript reactjs graphql apollo react-apollo