Skip to content

Instantly share code, notes, and snippets.

@scottdixon
Created November 21, 2024 03:47
Show Gist options
  • Save scottdixon/7a7c5ea5192615d96ea8de0111d1e4e5 to your computer and use it in GitHub Desktop.
Save scottdixon/7a7c5ea5192615d96ea8de0111d1e4e5 to your computer and use it in GitHub Desktop.
import * as React from 'react';
import {Pagination} from '@shopify/hydrogen';
/**
* <PaginatedResourceSection > is a component that encapsulate how the previous and next behaviors throughout your application.
*/
export function PaginatedResourceSection<NodesType>({
connection,
children,
resourcesClassName,
namespace,
}: {
connection: React.ComponentProps<typeof Pagination<NodesType>>['connection'];
children: React.FunctionComponent<{node: NodesType; index: number}>;
resourcesClassName?: string;
namespace?: string;
}) {
return (
<Pagination connection={connection} namespace={namespace}>
{({nodes, isLoading, PreviousLink, NextLink}) => {
const resoucesMarkup = nodes.map((node, index) =>
children({node, index}),
);
return (
<div>
<PreviousLink>
{isLoading ? 'Loading...' : <span>↑ Load previous</span>}
</PreviousLink>
{resourcesClassName ? (
<div className={resourcesClassName}>{resoucesMarkup}</div>
) : (
resoucesMarkup
)}
<NextLink>
{isLoading ? 'Loading...' : <span>Load more ↓</span>}
</NextLink>
</div>
);
}}
</Pagination>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment