React Speech Recognition Hook

4.0.1 · active · verified Sun Apr 19

react-speech-recognition is an open-source React library that simplifies integrating browser-based speech recognition into React applications. It provides a `useSpeechRecognition` hook and a `SpeechRecognition` object, abstracting the complexities of the underlying Web Speech API to enable real-time transcription from a user's microphone. The library is currently stable at version 4.0.1, with releases occurring as needed to address bugs or add features. It is actively maintained. Key differentiators include its easy-to-use hook API, built-in handling for browser support detection, microphone access states, and a mechanism for defining voice commands. While it leverages the Web Speech API, which has best support in Chromium-based browsers, the library itself is designed for broad compatibility, recommending polyfills for wider cross-browser functionality.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic speech-to-text functionality, including starting/stopping recognition, resetting the transcript, handling browser support, microphone availability, and defining simple voice commands.

import React from 'react';
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition';

const Dictaphone = () => {
  const commands = [
    {
      command: ['hello', 'hi'],
      callback: () => alert('Hello there!')
    },
    {
      command: 'open * website',
      callback: (website) => {
        window.open(`https://www.${website.split(' ').join('')}.com`);
      }
    },
    {
      command: ['clear', 'reset'],
      callback: () => resetTranscript()
    }
  ];

  const { 
    transcript, 
    listening, 
    resetTranscript, 
    browserSupportsSpeechRecognition, 
    isMicrophoneAvailable,
    finalTranscript
  } = useSpeechRecognition({
    commands,
    continuous: false // Set to true for continuous listening, but be aware of browser quirks
  });

  if (!browserSupportsSpeechRecognition) {
    return <span>Browser doesn't support speech recognition. Consider a polyfill.</span>;
  }

  if (!isMicrophoneAvailable) {
    return <span>Microphone is not available or permission was denied.</span>;
  }

  return (
    <div>
      <p>Microphone: {listening ? 'ON' : 'OFF'}</p>
      <button onClick={() => SpeechRecognition.startListening({ language: 'en-US' })}>Start</button>
      <button onClick={SpeechRecognition.stopListening}>Stop</button>
      <button onClick={resetTranscript}>Reset Transcript</button>
      <p>Transcript: {transcript}</p>
      <p>Final Transcript: {finalTranscript}</p>
    </div>
  );
};

export default Dictaphone;

view raw JSON →