Skip to main content

Predictive search store

The createPredictiveSearchStore function creates a store that searches as the customer types and holds the latest results. Use the store to build search-as-you-type UI in DOM code or in UI frameworks without Hydrogen bindings.

React and Vue apps use PredictiveSearchProvider.

Import createPredictiveSearchStore from @shopify/hydrogen.


Creates a store that searches as the customer types and holds the latest predictive search results. Use the store in DOM code or in UI frameworks without Hydrogen bindings. In React and Vue, use PredictiveSearchProvider.

Call search() with each new term, read results with getState() or subscribe(), and call destroy() when the search UI unmounts. By default, the store requests results from the route that createPredictiveSearchServerHandlers serves.

The function throws an error when you omit the fetch option in a runtime without a global fetch.

Anchor to options
options
Default: {}

Endpoint, timing, and search settings for the store.

<TData extends = >

A store with no term and empty results, ready to search.

Examples

TypeScript

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

const store = createPredictiveSearchStore({
debounceInMs: 200,
minTermLength: 2,
limit: 5,
types: ["PRODUCT", "QUERY"],
});

const unsubscribe = store.subscribe((state) => {
if (state.status === "error") console.error(state.error);
if (state.status === "success") console.log(state.result.items.products);
});

await store.search("snowboard");

unsubscribe();
store.destroy();

Was this page helpful?