Skip to main content

Collection store

The createCollectionStore function creates a store that holds the customer's filter and sort choices for a collection or search page. Use the store to change filters and sort from your UI, navigate to the new URL after each change, and show a loading state until your loader returns new results.

In React and Vue apps, use CollectionProvider, which creates the store for you.

Import createCollectionStore, createCollectionReconciler, and createInitialCollectionState from @shopify/hydrogen.


Anchor to Filter and sort storeFilter and sort store

Creates a store that holds the customer's filter and sort choices for one collection. Your loader fetches the products.

The store reads its starting filters and sort from urlSearch, or from dataSearch when you omit urlSearch. In React, use CollectionProvider, which creates the store for you and keeps the store in sync with the URL.

Anchor to options
options
required

The collection data, the current URL search string, and the browse change callback.

A store with methods that change the filters and sort and report the loading status.

Examples

TypeScript

import { createCollectionStore } from "@shopify/hydrogen";

declare const loaderData: { collection: { handle: string; dataSearch: string } };
declare function navigate(to: { search: string }): Promise<void>;
declare function renderFilters(filters: unknown[]): void;

function buildBrowseSearch(): string {
const params = new URLSearchParams(window.location.search);
for (const key of Array.from(params.keys())) {
if (key === "sort_by" || key.startsWith("filter.")) params.delete(key);
}
params.delete("before");
params.delete("after");
for (const [key, value] of store.serializeToParams()) params.append(key, value);
return params.toString();
}

const store = createCollectionStore({
data: loaderData.collection,
urlSearch: window.location.search,
onBrowseChange: async () => {
await navigate({ search: buildBrowseSearch() });
store.settle();
},
});

store.subscribe((state) => renderFilters(state.filters));

Creates a reconciler that keeps the URL, your loader data, and a collection store in sync while the customer changes filters and sort.

Use a reconciler to connect a collection store to a router that has no Hydrogen binding. In React, use CollectionProvider.

Anchor to callbacks
callbacks
required

The functions that return the store and the URL search string and navigate your router.

Anchor to initialPrevUrlSearch
initialPrevUrlSearch
string
Default: ""

The current URL search string, which the first reconcile() call compares against. Defaults to an empty string.

A reconciler. Call reconcile() on URL changes, and register handleBrowseChange() as the store's browse change callback.

Examples

TypeScript

import { createCollectionReconciler, createCollectionStore } from "@shopify/hydrogen";

declare const data: { handle: string; dataSearch: string };
declare const router: {
getSearch: () => string;
navigate: (to: string) => void;
};

const store = createCollectionStore({ data, urlSearch: router.getSearch() });
const reconciler = createCollectionReconciler(
{
getStore: () => store,
readUrlSearch: router.getSearch,
emitChange: (search) => router.navigate(`${location.pathname}${search}`),
},
router.getSearch(),
);
store.setOnBrowseChange(() => reconciler.handleBrowseChange());

export function onRouteLoaded(dataSearch: string) {
reconciler.reconcile(router.getSearch(), dataSearch);
}

Creates collection state with no filters and the default sort for a collection handle.

Anchor to handle
handle
string
required

Collection handle, such as shoes.

A state with no filters, the collection's default sort, an ascending direction, and an idle status.

Examples

TypeScript

import { createInitialCollectionState, serializeCollectionParams } from "@shopify/hydrogen";

const state = createInitialCollectionState("shoes");

const search = serializeCollectionParams(state).toString();

Was this page helpful?