Guides

React integration

Two ways to use the SDK from React: an ARView held in a ref for product viewers, and the useARCore hook when you want engine state in React state.

There are two React-shaped ways to use the SDK. Pick based on how much of the engine you want mirrored into React state:

  • createARView() in a ref, the pattern the repository's example uses. Best for product viewers: the engine runs imperatively, React only tracks UI state (active product, placed or not, current scale).
  • useARCore(), a hook that wraps the engine and mirrors support, session, placed models, planes, surface state and errors into React state.

Product viewer with createARView

ProductAR.jsx
import { useEffect, useRef, useState } from 'react';
import { createARView } from 'reticlexr';
 
const MODELS = {
  Sofa: '/models/sofa.glb',
  Chair: '/models/chair.glb',
};
 
export default function ProductAR() {
  const overlayRef = useRef(null);
  const arRef = useRef(null);
  const [active, setActive] = useState('Sofa');
 
  useEffect(() => {
    const ar = createARView({ overlay: overlayRef.current });
    arRef.current = ar;
    return () => {
      ar.destroy().catch(() => {});
      arRef.current = null;
    };
  }, []);
 
  async function start() {
    const ar = arRef.current;
    await ar.create({ showReticle: true, showPlaneDots: true });
    await ar.preloadModels(Object.values(MODELS));
    await ar.placeModel(MODELS[active]);
  }
 
  async function switchTo(name) {
    setActive(name);
    // switchModel, NOT placeModel, keeps placement, scale and rotation.
    await arRef.current?.switchModel(MODELS[name]);
  }
 
  return (
    <>
      <button onClick={start}>View in your room</button>
      <div id="ar-overlay" ref={overlayRef}>
        {Object.keys(MODELS).map((name) => (
          <button key={name} data-ui onClick={() => switchTo(name)}>
            {name}
          </button>
        ))}
      </div>
    </>
  );
}

Three details matter:

  1. One ARView per mount, destroyed in the cleanup. destroy() ends the XR session and removes the placed models. Under React StrictMode's dev double-mount you will build a throwaway engine instance, harmless in production, noticeable as extra startup cost in dev.
  2. data-ui on interactive overlay children. The gesture layer ignores pointer events on elements matching [data-ui], so buttons don't also place or drag the model.
  3. switchModel for switching. The older README example used placeModel here with a comment claiming state carries over; it does not. placeModel performs a fresh centre hit test and adds a new model (the old one stays placed: list them with getPlacedModels(), remove them with clearModels()). See Model preloading & switching.

The useARCore hook

HookExample.jsx
import { useARCore } from 'reticlexr/react';
 
export default function HookExample({ overlayRef }) {
  const ar = useARCore();
 
  if (ar.isSupported === null) return <p>Checking AR support…</p>;
  if (ar.isSupported === false) return <p>AR is not available here.</p>;
 
  return (
    <div>
      <button
        onClick={() => ar.startSession({ domOverlay: overlayRef.current })}
        disabled={ar.isSessionActive}
      >
        Start AR
      </button>
      <button onClick={() => ar.placeModel('/models/sofa.glb')}>
        Place sofa
      </button>
      <p>
        Planes: {ar.detectedPlanes.total} · Surface:{' '}
        {ar.surfaceState.hasSurface ? ar.surfaceState.planeType : 'none'}
      </p>
      {ar.error && <p role="alert">{ar.error}</p>}
    </div>
  );
}

The hook returns state (isSupported, isSessionActive, placedModels, detectedPlanes, surfaceState, error) and methods mirroring the engine; the full list is in the API reference.

Warning

Two hook facts to design around:

  • Shared engine by default. All useARCore() consumers share one lazily-constructed engine instance. When any one consumer unmounts, its cleanup stops the session for all of them. Mount the hook in exactly one place in your tree (or lift it into a provider you control), or pass a dedicated engine per view: useARCore({ engine }) with a new ARCoreWeb() from reticlexr/web.
  • Transform argument shapes are normalised. The hook's placeModel(modelUrl, position, rotation, scale) accepts arrays ([x, y, z], quaternion [x, y, z, w], [sx, sy, sz]) or {x, y, z} objects, and Euler-radian rotations; scales clamp to [0.1, 5]. (In v1.0.0, objects produced NaN transforms, fixed in 1.1.0.)

Overlays and SSR

Overlay children are regular React DOM rendered into the element you pass as overlay; the same element is handed to the XR session as its DOM overlay. Styling, routing and state inside it are entirely yours, see Overlays.

TODO: unverified

SSR: import-time side effects were removed in 1.1.0 (the event bridge installs lazily on first subscription; the shared hook engine is constructed lazily), and all five entry points pass DOM-less import checks, importing them touches no window/document, and the hook renders to string. A full framework prerender (e.g. Next.js) has not been exercised yet, so that remains TODO: until it is verified, loading AR components client-side only (e.g. Next.js dynamic(..., { ssr: false })) is the safe default, which is what this site's own demo does.