-
I'm currently building an app that has a paginated page. The initial data is populated through server-side rendering. const getQuery = (offset: number = 0, query: string): string => {
if (offset) {
offset += 1;
}
return gql`
query {
apps(name: "${escape(query)}", limit: 10, offset: ${offset}) {
data {
name
owner
url
}
count
}
}
`;
};
const { data, error } = useSWR<GraphQLResult, any>(
getQuery((page - 1) * 10, query),
graphQLFetcher,
{ initialData },
);
const showSearchResult = React.useCallback(() => {
if (!data) {
return ( // loading );
}
const apps = data.apps.data;
if (!apps.length) {
return ( // empty );
}
return (
// stuff
);
}, [ // deps ]); It works fine in general. However, when I tried to navigate it to the next page, the value of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I think you should go with You can check these examples: |
Beta Was this translation helpful? Give feedback.
-
Found the answer. I think I described my use case poorly. Basically, what I want is to apply the const { data, error } = useSWR<GraphQLResult, any>(
[gqlQuery, page],
(gqlQuery, page) => {
const variables = {
query,
limit: ITEM_PER_PAGE,
offset: (page - 1) * ITEM_PER_PAGE,
};
if (variables.offset) {
variables.offset += 1;
}
return graphQLFetcher<GraphQLVariables>(gqlQuery, variables);
},
{ initialData: page === 1 ? initialData : null },
); |
Beta Was this translation helpful? Give feedback.
Found the answer. I think I described my use case poorly.
Basically, what I want is to apply the
initialData
only to the first page. Therefore, I can implement a conditional whether the page is the first page or not.