Skip to Content
BetterFeedback Docs

Install in Next.js and React

Load the BetterFeedback embed script in a Next.js or React application, let the page own mounting with the JavaScript API, or render the element in JSX, with the queue stub that makes early calls safe.

There is no npm package and no React wrapper. The script tag and the <betterfeedback-widget> element are the API, and both work inside a React tree.

Let the page own mounting

Load the script once with data-render-on-load="false", so it registers the element but mounts nothing, and install the queue stub before it so calls made before the script has loaded are replayed in order.

app/layout.tsx
import Script from "next/script";

const stub = `window.betterfeedback=function(){(window.betterfeedback.q=window.betterfeedback.q||[]).push(arguments)}`;

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script id="betterfeedback-stub" strategy="beforeInteractive">{stub}</Script>
        <Script
          src="https://ask.betterfeedback.ai/widget.js"
          data-render-on-load="false"
          strategy="afterInteractive"
        />
      </body>
    </html>
  );
}

Mount from the page that needs the conversation and unmount when it leaves.

app/contact/page.tsx
"use client";

import { useEffect } from "react";

declare global {
  interface Window {
    betterfeedback: (name: string, arg?: unknown, extra?: unknown) => void;
  }
}

export default function ContactPage() {
  useEffect(() => {
    window.betterfeedback("mount", {
      survey: "YOUR_SURVEY_ID",
      target: "#contact-form",
      autoHeight: true,
      voice: false,
    });
    return () => window.betterfeedback("unmount");
  }, []);

  return <div id="contact-form" />;
}

mount takes any option by its JS key and reads the rest from the script tag's data-* attributes. It mounts nothing when an element already exists on the page, and unmount removes every widget element. The script sets origin from its own URL, so you never configure where the conversation loads from.

Render the element in JSX

React renders custom elements as they are, so the element can live in your markup instead. Attributes are the kebab-case names from Configuration.

<betterfeedback-widget survey="YOUR_SURVEY_ID" mode="inline" auto-height="" />

TypeScript needs to know the tag:

betterfeedback-widget.d.ts
declare namespace React.JSX {
  interface IntrinsicElements {
    "betterfeedback-widget": React.DetailedHTMLProps<
      React.HTMLAttributes<HTMLElement> & Record<string, string | undefined>,
      HTMLElement
    >;
  }
}

Load the script as above, without data-survey; a placed element always wins over auto-mounting.

Events and commands

With the stub in place, window.betterfeedback("on", "completed", handler) works from any component, before or after the script loads. JavaScript API lists the commands and the events.

On this page