【发布时间】:2020-01-19 08:20:17
【问题描述】:
我在使用 graphQL 时看到了一种反应场景。在下面的代码中,每当代码到达 onPaginationChange 方法时,它也会到达 getVariables 方法并最终调用 GraphQL 来获取数据。它是怎么发生的?
onPaginationChange 方法中的状态变化是否会导致所有依赖状态的地方刷新?是反应的东西吗?或者一些graphQL的东西?在下面的代码中,我提供了我认为有用的主要内容。我已经删除了 Table 的确切实现,因为我认为这个问题不需要回答。
import React, { Component } from "react";
import gql from "graphql-tag";
import { Query } from "react-apollo";
export const LIST_ITEMS = gql`
query list(
$pageIndex: Int
$limit: Int
) {
listItems(
limit: $limit
pageIndex: $pageIndex
) {
items {
name
description
}
totalCount
}
}
`;
export class ManageItems extends Component {
constructor(props) {
super(props);
this.state = {
currentPageIndex: 1,
pagesCount: 1,
pageSize: 30
};
}
getVariables() {
// It comes here whenever there onPaginationChange function is called. HOW?
return {
pageIndex: this.state.currentPageIndex - 1,
limit: this.state.pageSize
};
}
onPaginationChange({ detail }) {
this.setState({
currentPageIndex: detail.currentPageIndex,
pageSize: detail.pageSize
});
}
render() {
const { t } = this.props;
return (
<Query
query={LIST_ITEMS}
variables={this.getVariables()}}
>
{({ loading, error, data }) => {
if (error)
return ("error");
return (
<Table onPaginationChange={this.onPaginationChange.bind(this)}>
</Table>
);
}}
</Query>
);
}
}
export ManageItems;
【问题讨论】:
-
有什么好的文档或视频可以推荐阅读或观看吗?我在网上读了一些东西。很难理解/记住所有这些概念。有时,它们也没有被覆盖。