Hooks

useSpeechRecognition

Live speech-to-text for React Native with the same API as the web hook, backed by the optional expo-speech-recognition module.

iOSAndroid

Installation

pnpm dlx @docyrus/cli add @docyrus/rn-hooks-use-speech-recognition
Required Packages(1 package)
pnpm add expo-speech-recognition

expo-speech-recognition is an optional peer with a config plugin, so it needs a development build — see Speech Recognition for the setup. The module is loaded lazily with a guarded require(): in Expo Go (or when it isn't installed) isSupported is false, start / stop are no-ops, and DocyrusAgentChatInputMicButton hides itself. start() asks for microphone + speech permissions first; a denial sets error to 'not-allowed'.

Usage

import {
  DocyrusAgentChatInput,
  DocyrusAgentChatInputDefaultBody,
  DocyrusAgentChatInputMicButton,
  useDocyrusAgentMicTranscription
} from '@/components/docyrus-native/docyrus-agent';
import { useSpeechRecognition } from '@/hooks/docyrus-native/use-speech-recognition';

function MicTool() {
  const mic = useDocyrusAgentMicTranscription();
  const speech = useSpeechRecognition({ lang: 'en-US', ...mic.speechHandlers });

  return <DocyrusAgentChatInputMicButton isRecording={speech.isRecording} isSupported={speech.isSupported} onToggle={speech.toggle} />;
}

<DocyrusAgentChatInput>
  <DocyrusAgentChatInputDefaultBody extraTools={<MicTool />} />
</DocyrusAgentChatInput>

DocyrusAgentChatInputMic is the same thing pre-wired.

API Reference

UseSpeechRecognitionArgs

OptionTypeDefaultDescription
langstringdevice localeBCP-47 tag (en-US, tr-TR)
continuousbooleantrueKeep listening after each result
interimResultsbooleantrueStream partial transcripts
onTranscript(chunks: { final: string; interim: string }) => void-Every result: cumulative final text + pending interim
onStart() => void-Session started
onEnd() => void-Session ended (stop, error or timeout)

UseSpeechRecognitionResult

FieldTypeDescription
isSupportedbooleanModule installed, linked and recognition available
isRecordingbooleanSession active
transcriptstringCumulative final transcript of the session
start / stop / toggle() => voidControl the session
reset() => voidClear the transcript
errorstring | nullLast native error code ('not-allowed', 'no-speech', …)

isSpeechRecognitionAvailable() is exported as a plain function for non-hook checks.

On this page