Developers
Embed Lifecycle: Mount and Destroy
Manage the UPCS embed lifecycle in single-page apps: the handle returned by UPCS.mount, calling destroy, remounting and avoiding duplicate iframes.
Updated 14 October 2026 · 2 min read
UPCS.mount returns a handle: { iframe, destroy() }. In a static page you can ignore it. In a single-page application, where components appear and disappear without a full page load, it is how you keep the embed tidy.
The handle
const handle = UPCS.mount('#upcs-map', { sessionToken: token, height: '500px' });
handle.iframe; // the iframe element, useful for layout or testing
handle.destroy(); // remove the map
When to destroy
- Route change. The user navigates away; the component that owns the map unmounts.
- Remount. You need a new session after expiry; destroy the old map before mounting a new one.
- Conditional UI. The map sits in a tab or modal that is hidden and later shown again.
- Development reloads. Hot module reloading can mount a component twice.
Skipping destroy() can leave orphan iframes, duplicate callbacks, and in some cases two maps in the same container.
Pattern: one owner
Keep the handle in the same scope as the code that created it, and make creation idempotent:
let handle = null;
function showMap(token) {
if (handle) handle.destroy();
handle = UPCS.mount('#upcs-map', {
sessionToken: token,
onSelect,
onError
});
}
function hideMap() {
if (handle) { handle.destroy(); handle = null; }
}
Frameworks
- React: call
destroy()in the cleanup function ofuseEffect; see using the UPCS embed in React. - Vue: call it in
onBeforeUnmount; see using the embed in Vue.
Combine with session renewal
After session_expired, destroy, fetch a new token and mount again, as described in handle onError and session_expired. Keep any selection the user already made in your own state, not inside the map, so remounting loses nothing.
For the options accepted on mount, see UPCS.mount options explained.
Frequently asked questions
What does UPCS.mount return?
An object with the iframe element and a destroy() method.
When must I call destroy?
When your page or component is removed, or before mounting again into the same container.
