Skip to main content

getPaginationVariables
utility

Caution

This component is in an unstable pre-release state and may have breaking changes in a future release.

The getPaginationVariables function is used with the <Pagination> component to generate the variables needed to fetch paginated data from the Storefront API. The returned variables should be used within your storefront GraphQL query.

Request
required

The request object passed to your Remix loader function.

{ pageBy: number; }
Default: {pageBy: 20}

Options for how to configure the pagination variables. Includes the ability to change how many nodes are within each page.

Was this section helpful?

Example code

import {json} from '@shopify/remix-oxygen';
import {
Pagination__unstable as Pagination,
getPaginationVariables__unstable as getPaginationVariables,
} from '@shopify/hydrogen';
import {useLoaderData, Link} from '@remix-run/react';

export async function loader({request, context: {storefront}}) {
const variables = getPaginationVariables(request, {pageBy: 8});

const data = await storefront.query(ALL_PRODUCTS_QUERY, {
variables,
});

return json({products: data.products});
}

export default function List() {
const {products} = useLoaderData();

return (
<Pagination connection={products}>
{({nodes, PreviousLink, NextLink}) => (
<>
<PreviousLink>Previous</PreviousLink>
<div>
{nodes.map((product) => (
<Link key={product.id} to={`/products/${product.handle}`}>
{product.title}
</Link>
))}
</div>
<NextLink>Next</NextLink>
</>
)}
</Pagination>
);
}

const ALL_PRODUCTS_QUERY = `#graphql
query AllProducts(
$country: CountryCode
$language: LanguageCode
$first: Int
$last: Int
$startCursor: String
$endCursor: String
) @inContext(country: $country, language: $language) {
products(first: $first, last: $last, before: $startCursor, after: $endCursor) {
nodes { id
title
handle
}
pageInfo {
hasPreviousPage
hasNextPage
startCursor
endCursor
}
}
}
`;