useMemo so they are not recreated every render), wrap your tree with Dictation Provider, and render Dictation when the user should dictate.
Let’s get started!
Prerequisites
Before you begin, you must have the following requirements met:- Packages: Install
@suki-sdk/react,@suki-sdk/core. - Partner credentials: Obtain
partnerIdandpartnerTokenfrom Suki after Partner onboarding. - Browser and layout: Your app runs in a browser, with a real DOM and enough layout for the dictation UI.
Install the packages
Add the following packages to the same project you load in the browser:Step 1: Add container and field layout
For this example we will assume you are usingin-field mode as your preferred mode.
In in-field mode you need two parts in the UI: a div (or similar) that you can pass as rootElement, where the dictation controls show up, and a textarea (or input) that onSubmit will update when the user finishes.
The dictation layer matches the size of rootElement when you set it, so that node should have a height you control. Refer to Wrapper layout guide for recommended markup and CSS.
Code example for container and field layout
React
value, onChange, and your start button in the same component (see Step 5).
Step 2: Create auth manager
Now you need to create the Auth Manager for your application. To create the Auth Manager, you must have thepartnerId and partnerToken credentials from Suki.
Refer to Partner onboarding guide to learn how to get these credentials.
In React you usually create the auth manager inside the same useMemo as the dictation client (see Step 3), so both exist once per page or shell. The snippet below shows only the auth piece for clarity.
Code example for creating auth manager
JavaScript
Step 3: Create dictation client
After you have created the auth manager, you can create the dictation client for your application. The Dictation Client is the object the React components use under the hood. Create one dictation client and reuse it for the whole tree underDictationProvider. Use useMemo with an empty dependency array so you do not run new DictationClient(...) on every render.
Code example for creating dictation client in React
React
Step 4: Wrap with DictationProvider
Pass theclient from Step 3 into DictationProvider. Every component that renders <Dictation> must be a child of that provider.
Code example for wrapping with DictationProvider
React
DictationProvider at app root or only around the screen that needs dictation.
Step 5: Render Dictation
Render<Dictation> only while that field should own the session (for example when the user clicked “Start dictation”). When <Dictation> unmounts, the SDK calls hide() for you.
Pass mode, fieldId, initialText, onSubmit, and any optional props you need. Refer to Configuration guide for more details on the available options and how to use them.
Code example for rendering Dictation in React
React
onSubmit is required for a good user experience. If you omit it, dictation often closes immediately after the user acts.Callbacks
Props such asonSubmit, onCancel, and onDraft receive the same shape as in the JavaScript API:
React
You do not poll for text; the SDK calls your functions when the user commits or cancels, and may call
onDraft when they move on without submitting (refer to Callbacks guide for more details).Switching fields
Use the sameclient and DictationProvider. Change which field is active by updating state (for example activeFieldId) so only one <Dictation> is mounted, or swap fieldId / initialText on the next mount. You do not need to call hide() yourself just to move from one field to another when unmounting one <Dictation> and mounting another is how your UI switches.
Unmounting and hiding
When<Dictation> unmounts, the SDK automatically calls hide(). You do not need to call hide() yourself for normal React flows.
Complete code example
Below is a complete code example in React for in-field mode withDictationProvider, a toggle button, and one field.
React
notes through onSubmit, and only one <Dictation> is mounted at a time for that client.