Skip to main content

@xaiku/react

React components and hooks for integrating Xaiku into React applications. Built on top of @xaiku/browser, this package provides a context-based provider, experiment scoping, text rendering with variant resolution, and tracking hooks.

For Next.js applications, use @xaiku/nextjs which re-exports everything from this package plus server-side support.

Installation

npm install @xaiku/react

Usage

import makeSDK, { XaikuProvider, Experiment, Text } from '@xaiku/react';

function App() {
return (
<XaikuProvider pkey="pk_your_public_key">
<Experiment id="exp_1">
<Text id="headline" fallback="Default Headline" />
</Experiment>
</XaikuProvider>
);
}

Factory Function

makeSDK(options?: SDKOptions): BrowserSDKInstance

Creates a browser SDK instance with framework set to 'react' and frameworkVersion set to the installed React version. Accepts the same options as @xaiku/browser.

import makeSDK from '@xaiku/react';

const sdk = makeSDK({ pkey: 'pk_your_public_key' });

Components

XaikuProvider

The root provider that initializes the SDK and makes it available to all child components via React context.

<XaikuProvider pkey="pk_your_public_key">
{children}
</XaikuProvider>

Props:

PropTypeRequiredDescription
pkeystringYes (unless sdk is provided)Your public key.
sdkSDKInstanceNoPre-initialized SDK instance. When provided, the provider uses it instead of creating a new one.
childrenReactNodeYesChild components.
...restobjectNoAdditional options passed to makeSDK() (e.g., userId, orgId, experimentIds).

The SDK is initialized inside a useEffect to ensure it only runs in the browser. If an sdk prop is provided, it is used directly without creating a new instance.

Experiment

Scopes child components to a specific experiment ID via context, eliminating the need to pass experimentId to every Text component or tracking hook.

<Experiment id="exp_1">
<Text id="headline" fallback="Default" />
<Text id="subheadline" fallback="Default sub" />
</Experiment>

Props:

PropTypeRequiredDescription
idstringYesThe experiment ID.
childrenReactNodeYesChild components that inherit this experiment ID.

Text

Renders variant text for a given experiment and part. Automatically tracks a view impression when rendered.

{/* Basic usage with fallback */}
<Text id="headline" experimentId="exp_1" fallback="Default Headline" />

{/* Using Experiment context */}
<Experiment id="exp_1">
<Text id="headline" fallback="Default Headline" />
</Experiment>

{/* Render prop pattern */}
<Text id="headline" experimentId="exp_1">
{(text) => <h1>{text}</h1>}
</Text>

{/* Element children pattern */}
<Text id="headline" experimentId="exp_1">
<h1>Default Headline</h1>
</Text>

Props:

PropTypeRequiredDescription
idstringYesPart ID within the experiment variant.
experimentIdstringNoExperiment ID. Falls back to the nearest Experiment context.
fallbackReactNodeNoContent to display before variants load or if no variant text is found.
controlbooleanNoWhen true, renders the control variant instead of the selected variant.
childrenstring | function | ReactElementNoDetermines rendering behavior (see below).

Children behavior:

  • String: Used as the fallback text.
  • Function: Called with the resolved text as argument — (text) => ReactNode.
  • React Element: Cloned with data-xaiku-experimentid and data-xaiku-partid attributes, and the resolved text replaces the element's children.
  • Omitted: Renders a <span> with tracking data attributes.

Hooks

useText(experimentId, id, fallback, control)

Resolves variant text for a experiment and part. Re-renders the component when variants are selected.

function Headline() {
const text = useText('exp_1', 'headline', 'Default');
return <h1>{text}</h1>;
}

Parameters:

ParameterTypeDescription
experimentIdstringExperiment ID.
idstringPart ID within the variant.
fallbackanyValue returned while the SDK is loading or if no variant text exists.
controlbooleanWhen true, returns the control variant text.

Returns: The resolved variant text, or fallback if the SDK is not yet ready.

useExperimentId()

Returns the experiment ID from the nearest Experiment context.

function ChildComponent() {
const experimentId = useExperimentId();
}

useTrackView({ experimentId, variantId, partId })

Tracks a view impression when the component mounts. Each unique combination of experimentId, variantId, and partId is tracked only once per session.

function Hero({ experimentId }) {
useTrackView({ experimentId, partId: 'hero' });
return <div>...</div>;
}

Parameters:

ParameterTypeRequiredDescription
experimentIdstringYesExperiment ID.
variantIdstringNoVariant ID. Auto-resolved from the SDK if omitted.
partIdstringNoPart identifier.

useTrackClick({ experimentId, variantId, partId })

Returns a callback function that tracks a click event when called.

function CTAButton({ experimentId }) {
const trackClick = useTrackClick({ experimentId, partId: 'cta' });
return <button onClick={trackClick}>Sign Up</button>;
}

Parameters:

ParameterTypeRequiredDescription
experimentIdstringYesExperiment ID.
variantIdstringNoVariant ID. Auto-resolved from the SDK if omitted.
partIdstringNoPart identifier.

Returns: () => void — a memoized callback to attach to event handlers.

useTrackConversion({ experimentId, variantId, partId, value })

Returns a callback function that tracks a conversion event when called.

function CheckoutButton({ experimentId }) {
const trackConversion = useTrackConversion({
experimentId,
partId: 'checkout',
value: 49.99,
});

const handlePurchase = () => {
processPayment();
trackConversion();
};

return <button onClick={handlePurchase}>Buy Now</button>;
}

Parameters:

ParameterTypeRequiredDescription
experimentIdstringYesExperiment ID.
variantIdstringNoVariant ID. Auto-resolved from the SDK if omitted.
partIdstringNoPart identifier.
valuenumberNoConversion value (e.g., purchase amount).

Returns: () => void — a memoized callback to attach to event handlers.

Contexts

XaikuContext

React context that holds the SDK instance. Use useContext(XaikuContext) to access the SDK directly, or use the provided hooks instead.

import { useContext } from 'react';
import { XaikuContext } from '@xaiku/react';

function CustomComponent() {
const sdk = useContext(XaikuContext);
}

ExperimentContext

React context that holds the current experiment ID set by the nearest Experiment provider. Use useExperimentId() for convenient access.

import { useContext } from 'react';
import { ExperimentContext } from '@xaiku/react';

function CustomComponent() {
const experimentId = useContext(ExperimentContext);
}