Developers

    Use the UPCS Embed in React

    An illustrative React pattern for the UPCS embeddable map: fetch a session token, mount in an effect, handle callbacks and destroy on unmount.

    Updated 14 October 2026 · 2 min read

    There is no official React package for UPCS. What follows is an illustrative implementation pattern: a component that loads the documented embed script, mounts the map with a session token from your server, and cleans up on unmount.

    The component

    import { useEffect, useRef } from 'react';
    
    const SCRIPT_SRC = 'https://api.upcs.io/api/embed/upcs-embed.js';
    
    function loadScript() {
      if (window.UPCS) return Promise.resolve();
      return new Promise((resolve, reject) => {
        const s = document.createElement('script');
        s.src = SCRIPT_SRC;
        s.onload = resolve;
        s.onerror = reject;
        document.head.appendChild(s);
      });
    }
    
    export function UpcsMap({ onSelect, height = '500px' }) {
      const ref = useRef(null);
    
      useEffect(() => {
        let handle;
        let cancelled = false;
    
        async function start() {
          await loadScript();
          const res = await fetch('/upcs-session');
          const { session_token } = await res.json();
          if (cancelled || !ref.current) return;
    
          handle = window.UPCS.mount(ref.current, {
            sessionToken: session_token,
            height,
            onSelect,
            onError: (err) => console.error('UPCS', err)
          });
        }
    
        start().catch(console.error);
        return () => {
          cancelled = true;
          if (handle) handle.destroy();
        };
      }, []);
    
      return <div ref={ref} />;
    }
    

    UPCS.mount accepts either a CSS selector or a DOM element, so the ref can be passed directly.

    Points to note

    Using it in a form

    Keep the selected code in component state and submit it with the rest of your form, then re-check it on the server; see handling the onSelect response.

    Frequently asked questions

    Is there an official React component?

    No. UPCS has no official SDK or React package. This page shows an illustrative implementation pattern using the documented script and UPCS.mount.

    Why call destroy in the cleanup?

    It removes the iframe and avoids duplicates when React remounts the component.