Guide

Add a popup to React

Free to use · no credit card · no pageview cap

This guide covers a client-rendered React app built with Vite or Create React App. It was not tested on a live deployment; the code is the minimal placement that matches the widget's install contract. For Next.js, use the Next.js guide instead.

Option 1: index.html

This is the simplest and the least likely to break. Vite uses index.html at the project root; Create React App uses public/index.html. Add both lines before the closing </body>:

<div data-popuploom="YOUR_WIDGET_ID"></div>
<script src="https://popuploom.com/embed/1.2.0/popuploom.js"></script>

The div is a mount point; React does not manage it. Keep it outside the root element, so a re-render cannot remove it.

Option 2: a component

Use a component when the widget id comes from configuration or the embed depends on a route. The effect loads the script once and the guard keeps React Strict Mode from adding it twice in development.

import { useEffect } from "react";

const SRC = "https://popuploom.com/embed/1.2.0/popuploom.js";

export function PopupLoom({ widgetId }) {
  useEffect(() => {
    if (!widgetId) return;
    if (document.querySelector('script[src="' + SRC + '"]')) return;
    const script = document.createElement("script");
    script.src = SRC;
    script.async = true;
    document.body.appendChild(script);
  }, [widgetId]);

  if (!widgetId) return null;
  return <div data-popuploom={widgetId} />;
}

Mount it once, near the root of the app:

<PopupLoom widgetId={import.meta.env.VITE_POPUPLOOM_ID} />

Do not mount it on every route. A second mount would add a second div, and the runtime may then initialise the same widget twice.

Why the guard matters. React Strict Mode runs effects twice in development. Without the existing-script check, the browser would inject the script twice and the widget could initialise twice. The guard is also useful when a hot reload re-runs the effect. Production still runs it once, so the check costs nothing.

Single-page routing

React Router and similar routers change the view without reloading the document. Triggers that depend on time, clicks or scrolling keep working. Page targeting is the part to test: navigate to a route through the UI and see whether the popup fires as it does on a direct load.

If it does not, either load that route directly, move the trigger to a click, or mount the widget on the specific route. Do not mount it in two places at once to “cover” the router.

Content Security Policy

If the app sends a CSP, allow https://popuploom.com in script-src and connect-src. A blocked configuration request usually shows as a console error and an invisible popup; a blocked style injection shows as an unstyled overlay.

Test checklist

  • Fresh build, private window, popup appears once.
  • Reload: no duplicate overlay and no duplicate script tag in the DOM.
  • Test response arrives in the lead inbox.
  • Route changes behave as expected for your trigger.
  • Production build, not only the dev server.

Removal

Remove the two lines or the component, rebuild and deploy. The saved popup remains in the studio.

Common questions

Should I put the snippet in index.html or use a component?

Index.html is simpler and loads the widget once for the whole app. A component is useful when the widget id comes from config or you need to mount it conditionally. If you use a component, guard against adding the script twice.

Why does the component run twice in development?

React Strict Mode intentionally runs effects twice in development to surface cleanup bugs. The guard that checks for an existing script tag keeps the widget from loading twice. Production runs the effect once.

Does the popup work with React Router?

Test it. Client-side navigation does not reload the document, so a widget loaded once may not re-evaluate page targeting on every route change. Triggers such as timer, click and scroll keep working.

Can I mount the widget in a server-rendered React app?

Not directly. The component touches the document inside useEffect, which does not run on the server, so it is safe to import, but the script still needs a browser. For Next.js, follow the Next.js placement instead.

Official references

Written by Shailu016, founder of PopupLoom. Checked on 17 November 2026; not tested on a live deployment. No ranking, traffic or conversion claim is made. Related: Next.js, Astro, static HTML and the general install guide.