React hooks

React hooks

The @kelpclick/client/react entry point - KelpProvider, useSearch, useBrowse, and context hooks for React 18+.

@kelpclick/client/react is the React adapter for the Kelp SDK. It wraps the framework-agnostic core in a context provider and a set of hooks that handle debouncing, pagination, facet selection, request cancellation, and analytics event emission automatically.

What it provides

ExportPurpose
KelpProviderContext provider. Wraps your app and makes the client, identity, and analytics tracker available to all hooks.
useSearchDebounced search hook with pagination, facets, and auto-emitted search events.
useBrowseBrowse hook (no text query) for collection and category pages.
useKelpClientReturns the KelpClient from context.
useKelpIdentityReturns the KelpIdentity from context.
useKelpAnalyticsReturns the analytics tracker from context.
useKelpQueryLow-level query hook used internally by useSearch and useBrowse.
useDebouncedValueGeneral-purpose debounce hook.

Requirements

React 18 or later is a peer dependency of @kelpclick/client/react. The rest of the SDK has no React dependency and runs in any JavaScript environment.

Installation

npm install @kelpclick/client

React is already in your project. No additional packages are needed.

Minimal end-to-end example

This is the pattern used in the vite-react example app. Create the client once at module scope, wrap your app in KelpProvider, then call useSearch in any descendant.

// kelp.ts - create once at module scope, never inside a component
import { createKelpClient } from '@kelpclick/client';

export const kelp = createKelpClient({
  searchUrl: import.meta.env.VITE_KELP_SEARCH_URL,
  analyticsUrl: import.meta.env.VITE_KELP_ANALYTICS_URL,
  apiKey: import.meta.env.VITE_KELP_API_KEY,
});
// main.tsx - wrap the app
import { KelpProvider } from '@kelpclick/client/react';
import { kelp } from './kelp';
import { App } from './App';

createRoot(document.getElementById('root')!).render(
  <KelpProvider client={kelp}>
    <App />
  </KelpProvider>,
);
// SearchBox.tsx - use the hook anywhere in the tree
import { useSearch } from '@kelpclick/client/react';
import type { KelpDocumentBase } from '@kelpclick/client';

interface Product extends KelpDocumentBase {
  title: string;
  handle: string;
}

export function SearchBox() {
  const { query, setQuery, hits, found, isLoading, error, trackProductClicked } =
    useSearch<Product>({ collection: 'products', perPage: 24 });

  return (
    <div>
      <input
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        placeholder="Search products..."
      />

      {isLoading && <p>Searching...</p>}
      {error && <p>Something went wrong.</p>}
      {!isLoading && <p>{found.toLocaleString()} results</p>}

      <ul>
        {hits.map((h) => (
          <li key={h.document.id}>
            <a
              href={`/products/${h.document.handle}`}
              onClick={() => trackProductClicked(h.document.id)}
            >
              {h.document.title}
            </a>
          </li>
        ))}
      </ul>
    </div>
  );
}

Create the client at module scope - not inside a component. Constructing createKelpClient(...) inline gives it a new identity on every render.

Next steps

  • KelpProvider - full provider config options
  • useSearch - debouncing, pagination, facets, click tracking
  • useBrowse - collection and category pages
  • Context hooks - useKelpClient, useKelpIdentity, useKelpAnalytics
  • Search - ranking, semantic search, and query parameters
  • Platforms - Hydrogen, Liquid, Magento, and SSR guides

On this page