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.
npm install react-speech-recognitionVerified import paths — ran on the pinned version, not inferred.
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.
Implement a polyfill (e.g., `web-speech-cognitive-services`) for broader browser support by calling `SpeechRecognition.applyPolyfill()` early in your application lifecycle. Provide fallback UI when not supported.
Ensure your application correctly handles the `browserSupportsSpeechRecognition` state, especially when integrating polyfills, to display appropriate fallback content or UI elements.
Check `isMicrophoneAvailable` and render appropriate UI feedback. Always initiate `startListening` in response to a direct user action, such as a button click, to comply with browser security policies.
Use `browserSupportsContinuousListening` to detect support. Avoid continuous listening on platforms where it's known to be problematic, or provide a 'push-to-talk' alternative.
Always deploy applications using `react-speech-recognition` over HTTPS. For local development on non-localhost IPs, you might need to configure browser flags or trust certificates.
Install `regenerator-runtime` (`npm i --save regenerator-runtime`) and import it at the very top of your application's entry file (e.g., `import 'regenerator-runtime/runtime';` in `_app.js` for Next.js or `index.js` for other React apps).
Ensure you `import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition';` and check `browserSupportsSpeechRecognition` before using the API. Consider applying a polyfill if cross-browser compatibility is needed.Deploy your application over HTTPS. Ensure the UI clearly prompts the user for microphone permission and handles the `isMicrophoneAvailable` state, guiding users to enable permissions if denied.