Skip to main content

useSpeech Hook

API Reference

Here is the full API for the useSpeech hook, these options can be passed as paramerters to the hook:

ParameterTypeRequiredDefaultDescription
textstring | JSX.ElementYes-It contains the text to be spoken by Web Speech API.
pitchnumber (0 to 2)No1The pitch at which the utterance will be spoken.
ratenumber (0.1 to 10)No1The speed at which the utterance will be spoken.
volumenumber (0 to 1)No1The volume at which the utterance will be spoken.
langstringNo-The language in which the utterance will be spoken.
voiceURIstring | string[]No-The voice using which the utterance will be spoken. If provided an array, further voices will be used as fallback if initial voices are not found. See possible values here.
highlightTextbooleanNofalseWhether the words in the text should be highlighted as they are read or not.
highlightPropsReact.DetailedHTMLPropsNo-Props to customize the highlighted word, typically applied to the <mark> tag.
preserveUtteranceQueuebooleanNofalseWhether to maintain a queue of speech utterances (true) or clear previous utterances (false).
onErrorSpeechSynthesisErrorHandlerNoconsole.errorFunction to be executed if browser doesn't support Web Speech API.
onStartSpeechSynthesisEventHandlerNo-Function to be executed when speech utterance is started.
onResumeSpeechSynthesisEventHandlerNo-Function to be executed when speech utterance is resumed.
onPauseSpeechSynthesisEventHandlerNo-Function to be executed when speech utterance is paused.
onStopSpeechSynthesisEventHandlerNo-Function to be executed when speech utterance is stopped.
onBoundarySpeechSynthesisEventHandlerNo-Function to be executed at specified boundaries during speech synthesis.
onQueueChangeQueueChangeEventHandlerNo-Function to be executed whenever queue changes.

Types

SpeechSynthesisErrorHandler

type SpeechSynthesisErrorHandler = (error: Error) => any;

SpeechSynthesisEventHandler

type SpeechSynthesisEventHandler = (event: SpeechSynthesisEvent) => any;

QueueChangeEventHandler

type SpeechUtterancesQueue = SpeechSynthesisUtterance[];
type QueueChangeEventHandler = (queue: SpeechUtterancesQueue) => any;