Skip to content

@braidlabs/react ​

React bindings for Braid. Provides a typed <BraidFragment> component over <fragment-slot>, property bridge wiring, and navigation synchronization hooks for any React router.


Installation ​

bash
npm install @braidlabs/react @braidlabs/core

Initialization ​

Initialize the custom element definitions in your React entry point (client-side):

tsx
import { initBraidReact } from '@braidlabs/react';

// Initialize Braid for React
initBraidReact();

Component Usage ​

tsx
import React, { useState } from 'react';
import { BraidFragment } from '@braidlabs/react';

export function BillingSection({ cartId }: { cartId: string }) {
  const [ready, setReady] = useState(false);

  return (
    <div className="billing-wrapper">
      <BraidFragment
        name="billing"
        props={{ cartId }}
        onReady={({ fragmentId }) => {
          console.log(`Fragment ${fragmentId} ready!`);
          setReady(true);
        }}
        onError={({ stage, fixHint }) => {
          console.error(`Braid error at ${stage}:`, fixHint);
        }}
        onFragmentEvent={({ type, detail }) => {
          if (type === 'invoice:paid') {
            console.log('Invoice payment received:', detail);
          }
        }}
      />
    </div>
  );
}

Host Navigation Synchronization ​

Braid preserves host purity and never monkey-patches window.history. Use useBraidHostNavigation to notify bound fragments when your React router performs a client-side transition:

tsx
import { useLocation } from 'react-router-dom';
import { useBraidHostNavigation } from '@braidlabs/react';

export function AppRouterSync() {
  const location = useLocation();

  // Signals fragments after router commit
  useBraidHostNavigation(location.key);

  return null;
}

Works seamlessly with TanStack Router, React Router, Next.js, and Remix.


Component Props Reference ​

PropTypeDescription
namestring (Required)The fragment ID registered in Braid Gateway.
srcstringFixed sub-route to render. If omitted, fragment follows host URL.
propsRecord<string, unknown>Structured-cloned properties passed across the realm boundary.
onReady({ fragmentId }) => voidTriggered when fragment completes mounting and hydration.
onError(error: BraidFragmentError) => voidTriggered on load/mount error with failure stage & fix hint.
onFragmentEvent({ type, detail }) => voidHandles custom events dispatched by the fragment.
onStateChange(state: FragmentSlotState) => voidObserves slot lifecycle (idle, loading, ready, error).
refRef<BraidFragmentHandle>Access to .reload() and .state.