React & Next.js (@tinydialog/sdk-js/react)
Initialize tinyDialog in React and Next.js apps using the @tinydialog/sdk-js/react subpath with support for server-rendered inline widgets and client-side navigation.
Use the @tinydialog/sdk-js/react subpath to initialize tinyDialog in React 18+ and Next.js apps—no <script> tag, no custom useEffect wrapper, and no separate React package needed.
1. Install @tinydialog/sdk-js
npm install @tinydialog/sdk-js
# or
pnpm add @tinydialog/sdk-jsreact (>=18) is declared as an optional peer dependency, so @tinydialog/sdk-js/react reuses your existing React installation without adding extra dependencies.
2. Mount <TinyDialog /> once in your root layout
Render <TinyDialog /> once near the root of your app:
You can render <TinyDialog /> directly inside a Server Component layout.tsx—it includes "use client" automatically.
import type {ReactNode} from 'react';
import {TinyDialog} from '@tinydialog/sdk-js/react';
export default function RootLayout({children}: {children: ReactNode}) {
return (
<html lang="en">
<body>
<TinyDialog />
{children}
</body>
</html>
);
}import {TinyDialog} from '@tinydialog/sdk-js/react';
export function App() {
return (
<>
<TinyDialog />
{/* your routes & components */}
</>
);
}Don't add the tinyDialog script tag as well
<TinyDialog /> replaces the <script> tag. If both are on the page, the script tag can mount inline widgets before React hydrates them, which can cause hydration errors.
3. Add widgets with HTML data attributes
Once <TinyDialog /> is mounted, place declarative widgets anywhere in your Server or Client Components:
// Inline embedded widget
export function PageFeedback({userEmail}: {userEmail?: string}) {
return (
<div
data-tinydialog-widgetcontainer="0181b447-1234-23d1-9675-z1a295cad5d7"
data-tinydialog-user={userEmail}
/>
);
}
// Popup widget trigger button (also use this for custom floating buttons)
export function FeedbackButton({userEmail}: {userEmail?: string}) {
return (
<button
data-tinydialog-openwidget="0181b447-1234-23d1-9675-z1a295cad5d7"
data-tinydialog-user={userEmail}
>
Give Feedback
</button>
);
}You do not need useEffect, key={pathname}, or manual bindSurveysWithDataAttributes() calls:
- Dynamically mounted widget containers and trigger buttons are detected automatically.
- Client-side route changes automatically refresh inline widgets with the new page URL (while ignoring
#hashanchor jumps). - Floating trigger buttons refresh with the current page URL when opened.
- Optional attributes like
data-tinydialog-usercan be set directly on each widget container or trigger button. (The automaticdata-tinydialog-floating-buttonattribute only applies to<script>tags—render your own styled<button data-tinydialog-openwidget="...">in React.)
Styling inline widgets
<TinyDialog /> renders the widget iframe in a shadow root on the container, after the container's own children. Page CSS selectors such as [data-tinydialog-widgetcontainer] iframe cannot reach it, so style the iframe with ::part(iframe) instead:
[data-tinydialog-widgetcontainer]::part(iframe) {
width: 100%;
}- You can style the container itself as usual, for example with a border or shadow. Don't give it a fixed
height: the widget resizes its iframe, and the container grows with it. - Use a regular element such as
<div>or<section>as the container. Custom elements (tag names with a hyphen) and elements that can't host a shadow root, such as<details>, get the iframe as a normal child instead. Style those with[data-tinydialog-widgetcontainer] > iframe.
See the HTML Installation Guide for more declarative widget options.
Programmatic usage
For custom triggers, extra metadata, or headless submissions, import functions from @tinydialog/sdk-js in your Client Components:
'use client';
import {showWidget} from '@tinydialog/sdk-js';
export function CustomFeedbackButton({userEmail}: {userEmail: string}) {
return (
<button
onClick={() =>
showWidget('0181b447-1234-23d1-9675-z1a295cad5d7', {
user: userEmail,
extraData: {plan: 'pro'},
})
}
>
Give Feedback
</button>
);
}Importing @tinydialog/sdk-js does not modify the DOM on load, so static imports are safe in SSR Client Components.
See the SDK guides for all programmatic APIs:
- Basic Usage (
showWidget,loadSurvey) - Sending Extra Context-Data (
extraData,user) - Submit Feedback Programmatically (
submitFeedback)
Last updated on
