Passing Form Context from React or Next.js
Last updated: October 26, 2026
In a React or Next.js app the Docket widget lives on window, so every call has to happen in the browser. That rules out server components and anything that runs during rendering. Keep the widget calls inside an event handler in a Client Component and the rest is the same pattern you would use on a plain HTML page.
What do you need before you start?
The Docket widget script loaded from your application layout or page.
A variable configured on the agent for each field you plan to send. See Setting Up Dynamic Context Ingestion.
The component marked
'use client', sowindowexists when your handler runs.
How do you build the form component?
'use client';
import { FormEvent, useState } from 'react';
export function DemoRequestForm() {
const [error, setError] = useState('');
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setError('');
if (!window.AISeller?.setContext || !window.AISeller?.showAndConnect) {
setError('The conversation experience is still loading.');
return;
}
const form = new FormData(event.currentTarget);
const result = await window.AISeller.setContext({
name: String(form.get('name') || ''),
email: String(form.get('email') || ''),
company: String(form.get('company') || '')
});
if (!result.success) {
setError(result.message);
return;
}
window.AISeller.showAndConnect();
}
return (
<form onSubmit={handleSubmit}>
<input name="name" autoComplete="name" required />
<input name="email" type="email" autoComplete="email" required />
<input name="company" autoComplete="organization" />
<button type="submit">Talk to Sales</button>
{error ? <p role="alert">{error}</p> : null}
</form>
);
}Read the fields you need by name. A FormData object can contain File entries, and Docket accepts only strings, numbers, booleans and null.
How do you type window.AISeller in TypeScript?
Declare the methods you use so TypeScript stops complaining and you get autocomplete.
declare global {
interface Window {
AISeller?: {
setContext(data: Record<string, string | number | boolean | null>): Promise<{
success: boolean;
message: string;
}>;
resetContext(): Promise<{ success: boolean; message: string }>;
showAndConnect(): void;
show(): void;
hide(): void;
hideAndDisconnect(): void;
};
}
}Keep the type optional. The script loads asynchronously, so window.AISeller is genuinely undefined for the first moments of the page.
What should happen on logout and route changes?
Call resetContext() from your client-side logout flow, before another person can start a conversation in the same browser.
await window.AISeller?.resetContext();Client-side navigation does not clear stored context on its own. When a route change moves the visitor into a different journey, set the complete new context object for that journey rather than assuming the previous values were dropped.
How do you check that it works?
Load the page in a private browser window.
Confirm the Docket script has finished loading.
Submit the form with test values.
Confirm the widget opens only after
setContext()returnssuccess: true.Ask the agent something that should use a submitted field.
Navigate between routes, log out, and confirm the next conversation starts clean.
Frequently asked questions
I get "window is not defined". What is wrong?
The code is running on the server. Add 'use client' to the component, and call the widget only from an event handler or an effect, never during render.
window.AISeller is undefined when the page first loads. Is that a bug?
No. The script loads asynchronously. Guard every call, as the example does, and the handler will simply do nothing until the widget is ready.
Can I call setContext() from a Server Action?
No. It is a browser API and encrypts the payload in the browser. Call it from client code, then run your Server Action.
Does context survive a client-side route change?
The pending context is not cleared by navigation, but it is consumed once a conversation starts. Treat each new journey as needing its own complete context object.
Why does setContext() return success: false on my typed form?
Something in the payload is not a simple value. Dates, arrays, nested objects and undefined all fail. The message names the field, so log result.message.