Skip to content

Framework Guides

Mcdavid Obioha edited this page Dec 2, 2026 · 2 revisions

Framework Guides

The widget is a client component: it uses state, effects and window. The package entry already has the "use client" directive. It needs React 18 or 19.

Next.js (App Router)

Render it in the root layout. You can import it directly into a Server Component, as long as you only pass plain values (strings, numbers, booleans, objects, arrays):

// app/layout.tsx
import { WhatsAppChat } from 'whatsapp-chat-widget-react';

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <WhatsAppChat phoneNumber="+12025550123" />
      </body>
    </html>
  );
}

Functions cannot cross from a Server Component to a Client Component. If you need onSend, messageTemplate, onOpenChange or other callbacks, wrap the widget in your own client component:

// components/Chat.tsx
'use client';
import { WhatsAppChat } from 'whatsapp-chat-widget-react';

export function Chat() {
  return <WhatsAppChat phoneNumber="+12025550123" onSend={(d) => console.log(d)} />;
}

hideOnPaths follows App Router navigation, so hideOnPaths={['/checkout/*']} works without reading the pathname yourself.

Next.js (Pages Router)

// pages/_app.tsx
import type { AppProps } from 'next/app';
import { WhatsAppChat } from 'whatsapp-chat-widget-react';

export default function App({ Component, pageProps }: AppProps) {
  return (
    <>
      <Component {...pageProps} />
      <WhatsAppChat phoneNumber="+12025550123" />
    </>
  );
}

Vite and Create React App

// src/App.tsx
import { WhatsAppChat } from 'whatsapp-chat-widget-react';

export default function App() {
  return (
    <>
      {/* ...routes... */}
      <WhatsAppChat phoneNumber="+12025550123" />
    </>
  );
}

Remix and React Router

Render it once in app/root.tsx, inside the body:

import { WhatsAppChat } from 'whatsapp-chat-widget-react';

export default function App() {
  return (
    <html lang="en">
      <head>{/* ... */}</head>
      <body>
        <Outlet />
        <WhatsAppChat phoneNumber="+12025550123" />
        <Scripts />
      </body>
    </html>
  );
}

Any other React setup

If your toolchain handles ESM packages and React, it works. Render the component once, high in the tree and outside any element with a CSS transform, filter or perspective, because those make position: fixed relative to that element instead of the screen.

The adapters subpath

createWebhookTransport and createOpenAICompatibleAI live in whatsapp-chat-widget-react/adapters. In Next.js, use the transport and AI objects from a client component (the same file as the widget, or one marked 'use client'), and create them at module level so the same object is used on every render. Keep AI provider keys in an API route, never in client code: see Backend Recipes. With TypeScript, the adapters path needs moduleResolution of node16, nodenext or bundler (the defaults in current Next.js and Vite templates).

Server rendering and hydration

The initial HTML contains the launcher and, if you set defaultOpen, the form. Business-hours status, page context, showAfter and hideOnPaths are all resolved after mount, so server and client markup match and React reports no hydration mismatch.

Content Security Policy

The widget adds one inline <style> element. If your CSP blocks inline styles, set injectStyles={false} and import whatsapp-chat-widget-react/styles.css so your bundler serves it as a normal stylesheet. See Styling and Theming.

Testing your own app

The component renders in jsdom (Vitest, Jest with ESM support). Business-hours status needs timers: use fake timers and set the system time. The widget refreshes the status once a minute.

Clone this wiki locally