Skip to content

Widget Hook and Provider

Mcdavid Obioha edited this page Dec 2, 2026 · 1 revision

Widget Hook and Provider

Available from v0.8.0 in the React package. The same methods exist on the global in the script-tag build.

isOpen and onOpenChange already let you control the widget from a parent (see Controlling the Widget). useWhatsAppWidget() goes further: any component can open it, close it, or open it with a pre-filled draft, without passing state down.

Setup

Wrap the widget and the components that need to control it in WhatsAppWidgetProvider:

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

function AskAboutPro() {
  const widget = useWhatsAppWidget();
  return (
    <button
      onClick={() =>
        widget.openWithDraft({ subject: 'Pro plan', message: 'I would like to know more about Pro.', agentId: 'sales' })
      }
    >
      Ask about Pro
    </button>
  );
}

export function Page() {
  return (
    <WhatsAppWidgetProvider>
      <WhatsAppChat agents={agents} />
      <AskAboutPro />
    </WhatsAppWidgetProvider>
  );
}

The provider is optional. <WhatsAppChat /> works on its own, exactly as before. You only need the provider when you use the hook. It adds under 0.5 kB gzipped, and it is a separate export, so it costs nothing if you do not import it.

useWhatsAppWidget()

Returns:

Member Type What it does
isOpen boolean true while the form is open, including when the visitor opened it (your component re-renders when it changes).
open() () => void Opens the form.
close() () => void Closes the form.
toggle() () => void Opens it if closed, closes it if open.
openWithDraft(draft) (draft: WidgetDraft) => void Opens the form, fills it in, and focuses the first empty field.
interface WidgetDraft {
  name?: string;
  subject?: string;
  message?: string;
  agentId?: string; // selects this agent when it exists in `agents`
}

Using the hook outside a WhatsAppWidgetProvider throws: useWhatsAppWidget() must be used inside <WhatsAppWidgetProvider>.

openWithDraft in detail

  • Opens the widget if it is closed, and returns the form to its normal view (for example after an AI instant reply).
  • Fills in only the fields you pass. Anything else the visitor already typed is kept.
  • Selects agentId if it matches an agent. An unknown id is ignored.
  • Focuses the first empty field, in order: name, subject, message. If everything is filled, the message box gets focus, so the visitor can read and edit it.
  • Fields hidden with fields={{ name: false }} are skipped.
  • During an active live chat there is no form to fill, so it just opens the conversation and ignores the draft.
  • It fills the form; it does not submit it. The visitor always reviews and sends.

Called before the widget is on screen

If you call open, toggle or openWithDraft while the widget has not appeared yet (for example showAfter is still waiting, or the widget has not mounted), the call is remembered and runs the moment the widget appears. Several calls run in order.

One widget per provider

The provider controls one widget. If you render two <WhatsAppChat> inside the same provider, the most recently mounted one is controlled. Use separate providers for separate widgets.

Recipes

Pricing buttons that start the right conversation

function PlanButton({ plan }: { plan: string }) {
  const { openWithDraft } = useWhatsAppWidget();
  return (
    <button onClick={() => openWithDraft({ subject: `${plan} plan`, message: `Hi, I'm interested in the ${plan} plan.` })}>
      Talk to sales about {plan}
    </button>
  );
}

A "Help" link in your nav that toggles the widget

function HelpLink() {
  const { toggle, isOpen } = useWhatsAppWidget();
  return <button aria-expanded={isOpen} onClick={toggle}>Help</button>;
}

Hide your own floating button while the widget is open

function BackToTop() {
  const { isOpen } = useWhatsAppWidget();
  return isOpen ? null : <button className="back-to-top">Top</button>;
}

Open after an event

const { openWithDraft } = useWhatsAppWidget();
useEffect(() => {
  if (paymentFailed) openWithDraft({ subject: 'Payment problem', message: `My payment failed (order ${orderId}).` });
}, [paymentFailed]);

Next.js

The provider and hook are client code ('use client' is included in the package). Put the provider in a client component and render it in your layout:

// app/providers.tsx
'use client';
import { WhatsAppWidgetProvider, WhatsAppChat } from 'whatsapp-chat-widget-react';

export function Providers({ children }: { children: React.ReactNode }) {
  return (
    <WhatsAppWidgetProvider>
      {children}
      <WhatsAppChat phoneNumber="+2348012345678" />
    </WhatsAppWidgetProvider>
  );
}

Without React

The script build exposes the same methods on window.WhatsAppChatWidget:

WhatsAppChatWidget.open();
WhatsAppChatWidget.close();
WhatsAppChatWidget.toggle();
WhatsAppChatWidget.openWithDraft({ subject: 'Pro plan', message: 'Tell me about Pro.' });
WhatsAppChatWidget.isOpen; // a property

See Script-Tag Build.

Troubleshooting

  • useWhatsAppWidget() must be used inside <WhatsAppWidgetProvider>. The component calling the hook is not inside the provider. Move the provider higher.
  • Nothing happens when I call open(). The widget is not rendered inside the same provider, or it is hidden or hidden by hideOnPaths. Calls wait until it appears.
  • The draft was not applied. An active live chat ignores drafts. Also check the agent id matches an entry in agents.
  • isOpen is false at first render. It reflects the widget once it has mounted.

Clone this wiki locally