React Integration
Choose one of three React integrations based on how your application creates its browser and host services. All three are private workspace packages.
Choose the integration path
| Package | Required owner | Scope |
|---|---|---|
| @charaweave/react | BrowserSession from @charaweave/browser with V10 Core projection | Standard React facade |
| @charaweave/react-browser | ConsumerSession + BrowserEnvironment from Platform V6 | Existing convenience composition |
| @charaweave/react-host-browser | Caller-owned registry, geometry and executor | Lower-level Host binding |
Do not combine providers for the same target. For new ordinary Core/Browser integrations, use the standard facade; choose the other paths when the corresponding composition is already owned by your application.
Standard Core/Browser React facade
Create the Core and Browser exactly as in Generic Web Integration, using the application's character Element and placement port. Instead of calling browser.attachHost manually for React-owned buttons, mount a provider and use its binding hook. Place this code in the app's main.tsx, and call the exported mount function from the route/application owner after creating Core and Browser.
import { CharaWeaveProvider, useCharaWeaveHost } from "@charaweave/react";
import type { CoreSession } from "@charaweave/core";
import type { BrowserSession } from "@charaweave/browser";
import { createRoot } from "react-dom/client";
import { flushSync } from "react-dom";
function PhotoCard({ id, onOpen }: { id: string; onOpen: () => void }) {
const ref = useCharaWeaveHost<HTMLButtonElement>({
id: "feed.photo." + id,
role: "photo-card",
capabilities: ["activate"],
onActivate: onOpen,
});
return <button ref={ref} onClick={onOpen}>Open photo {id}</button>;
}
export function mountCharacterRoute(
mount: Element, core: CoreSession, browser: BrowserSession,
onOpen: () => void,
) {
const root = createRoot(mount);
flushSync(() => root.render(
<CharaWeaveProvider browser={browser}>
<PhotoCard id="42" onOpen={onOpen} />
</CharaWeaveProvider>,
));
core.start();
if (browser.start().status !== "accepted") {
root.unmount(); browser.dispose(); core.dispose();
throw Error("Browser did not start");
}
return () => {
browser.suspend();
root.unmount(); // React detaches Host bindings
browser.dispose();
core.dispose();
};
}
The route provides the callback for opening the photo. A user click and a character activation can invoke the same function. A callback ref binds a committed Element, and unmount or semantic identity change detaches the old registration. The Provider does not create/start/dispose the session itself. The example assumes the route owns the supplied sessions; if an ancestor owns them, do not dispose them from the route. The V10 facade is not a general V11 CharacterSession SDK.
Path A: platform Browser helper
@charaweave/platform/browser creates a BrowserEnvironment for a ConsumerSession. @charaweave/react-browser exports ConsumerSessionProvider and useConsumerHost; they attach/detach semantic Host surfaces using that environment.
import { ConsumerSessionProvider, useConsumerHost } from "@charaweave/react-browser";
import type { ConsumerSession } from "@charaweave/platform";
import type { BrowserEnvironment } from "@charaweave/platform/browser";
import type { ReactNode } from "react";
declare function openPhoto(surfaceId: string): Promise<void>;
function PhotoCard({ id }: { id: string }) {
const ref = useConsumerHost<HTMLButtonElement>({
id: `feed.photo.${id}`,
role: "photo-card",
capabilities: ["activate"],
onActivate: ({ surfaceId }) => openPhoto(surfaceId),
});
return <button ref={ref} aria-label={`Open photo ${id}`}>Open photo</button>;
}
function CharacterHost({
session, browser, children,
}: {
session: ConsumerSession; browser: BrowserEnvironment; children: ReactNode;
}) {
return (
<ConsumerSessionProvider session={session} browser={browser}>
{children}
</ConsumerSessionProvider>
);
}
openPhoto is Consumer code. The helper path currently creates its composition using Content V6; it does not expose the full V11 repertoire surface as a one-call high-level API. Check Host Integration before selecting this path for V11-specific behavior.
Path B: existing React Host Production Adapter
@charaweave/react-host-browser exports CharaHostProvider and useCharaSurface. It binds React elements to caller-owned HostSurfaceRegistry, BrowserHostGeometryAdapter, and HostSurfaceActionExecutor services. The adapter owns React ref/effect attachment lifetime; the application still creates and disposes those services.
import { CharaHostProvider, useCharaSurface } from "@charaweave/react-host-browser";
import type { ReactNode } from "react";
import {
createHostSurfaceActionExecutor,
createHostSurfaceRegistry,
} from "@charaweave/host";
import { createBrowserHostGeometryAdapter } from "@charaweave/host-browser";
const registry = createHostSurfaceRegistry();
const geometry = createBrowserHostGeometryAdapter();
const actions = createHostSurfaceActionExecutor(registry);
const services = { registry, geometry, actions };
declare function openPhoto(surfaceId: string): Promise<void>;
function BoundPhoto({ id }: { id: string }) {
const ref = useCharaSurface<HTMLButtonElement>({
id: `feed.photo.${id}`,
role: "photo-card",
capabilities: ["activate"],
onActivate: ({ surfaceId }) => openPhoto(surfaceId),
});
return <button ref={ref} aria-label={`Open photo ${id}`}>Open photo</button>;
}
function Feed({ children }: { children: ReactNode }) {
return <CharaHostProvider services={services}><BoundPhoto id="42" />{children}</CharaHostProvider>;
}
This example registers targets only. The application still needs a runtime, browser execution and renderer that support its package. Do not dispose shared services from an individual card component.
React lifecycle rules
- Bind exact committed Elements with callback refs; do not scan the DOM from Core code.
- Use stable semantic IDs, not list indexes. Change the ID when the product entity changes.
- Let the Adapter handle attach/detach; do not duplicate registration in effects.
- Own registry/geometry/action service lifetime above the subtree and dispose them after children detach.
- Keep handlers trusted and app-owned. The Adapter uses the latest committed callback while preserving its binding lifetime.
- Test SSR, hydration, hot reload and route changes in your application's actual mount model. Verify your application's actual mount model.
For React versions and acceptance boundaries, see React Production Integration and Host Integration.