React hooks

KelpProvider

Context provider that makes the Kelp client, identity, and analytics tracker available to all hooks in the tree.

KelpProvider is the root of the Kelp React integration. Wrap your app (or the subtree that uses Kelp hooks) in it. Every hook - useSearch, useBrowse, useKelpClient, useKelpIdentity, useKelpAnalytics - throws if called outside a provider.

Basic setup

import { createKelpClient } from '@kelpclick/client';
import { KelpProvider } from '@kelpclick/client/react';

// Create the client once at module scope.
const kelp = createKelpClient({
  searchUrl: 'https://{TENANT_ID}-search.kelp.click',
  analyticsUrl: 'https://{TENANT_ID}-events.kelp.click',
  apiKey: import.meta.env.VITE_KELP_API_KEY,
});

function App() {
  return (
    <KelpProvider client={kelp}>
      <YourRoutes />
    </KelpProvider>
  );
}

Props

interface KelpProviderProps {
  /** Required. The KelpClient created by createKelpClient(). */
  client: KelpClient;
  /** Optional. Created from default localStorage-backed storage when omitted. */
  identity?: KelpIdentity;
  /** Optional. Created from client + identity when omitted. */
  analytics?: KelpAnalytics;
  children: ReactNode;
}
PropRequiredNotes
clientyesThe KelpClient returned by createKelpClient(). Must be stable - create it at module scope or memoize it, never inline in a render.
identitynoA KelpIdentity from createIdentity(). When omitted, the provider creates one backed by localStorage. Pass your own to share a session with other parts of your app.
analyticsnoA KelpAnalytics tracker from createAnalytics(). When omitted, the provider creates and owns one. It handles close() and re-creation across React StrictMode double-mounts automatically.

What the provider creates automatically

When identity and analytics are not supplied, KelpProvider creates them internally:

  • identity - calls createIdentity() with default options (30-minute rolling session, localStorage storage).
  • analytics - calls createAnalytics({ client, identity }) with default flush settings (10 events or 5 seconds, whichever comes first). The provider calls close() on unmount and recreates the tracker on remount so analytics continues to flush correctly in React StrictMode.

Overriding identity and analytics

Pass your own instances to share state across providers or configure non-default options:

import { createKelpClient, createIdentity, createAnalytics } from '@kelpclick/client';
import { KelpProvider } from '@kelpclick/client/react';

const kelp = createKelpClient({ /* ... */ });

const identity = createIdentity({
  sessionTtlMs: 15 * 60 * 1000, // 15-minute session
  keyPrefix: 'mystore',          // isolate storage keys per tenant
});

const analytics = createAnalytics({
  client: kelp,
  identity,
  flushAt: 5,
  sanitizeEvent: (event) => ({ ...event, q: event.q ? '[redacted]' : event.q }),
});

function App() {
  return (
    <KelpProvider client={kelp} identity={identity} analytics={analytics}>
      <YourRoutes />
    </KelpProvider>
  );
}

When you pass your own analytics instance, the provider does not manage its lifecycle. Call analytics.close() yourself when the app unmounts, or rely on the pagehide flush the tracker registers automatically.

Nesting providers

Each KelpProvider creates its own independent context. Hooks read from the nearest ancestor provider. This allows different sub-trees to use different clients or collections, though most apps only need one top-level provider.

On this page