As I began working on my latest React project, I realized the importance of incorporating voice input to enhance user experience. This feature allows users to interact with the app using voice commands, which can be particularly useful for accessibility and convenience. In this post, I’ll walk you through how I implemented voice input in my React app using the react-speech-recognition library.
Thank me by sharing on Twitter 🙏
Setting Up Voice Recognition
To get started, I installed the react-speech-recognition package using npm.
npm install react-speech-recognition
npm install @types/react-speech-recognition --save-devNext, I imported the necessary components from the library.
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition';Implementing Voice Input
Here’s how I implemented voice input in my React app:
- Import and Use the Hook:
I used theuseSpeechRecognitionhook to manage speech recognition in my component.
import React from 'react';
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition';
const App = () => {
const { transcript, listening, browserSupportsSpeechRecognition } = useSpeechRecognition();
const { startListening, stopListening } = SpeechRecognition;
if (!browserSupportsSpeechRecognition) {
return <span>Browser doesn't support speech recognition.</span>;
}
return (
<div>
<textarea value={transcript} />
<button onClick={listening ? stopListening : startListening}>
{listening ? 'Stop' : 'Speak'}
</button>
</div>
);
};- Editing the Transcribed Text:
To allow editing of the transcribed text, I added anonChangeevent handler to the text area.
import React, { useState } from 'react';
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition';
const App = () => {
const { transcript, listening, browserSupportsSpeechRecognition } = useSpeechRecognition();
const { startListening, stopListening } = SpeechRecognition;
const [editedText, setEditedText] = useState('');
if (!browserSupportsSpeechRecognition) {
return <span>Browser doesn't support speech recognition.</span>;
}
useEffect(() => {
setEditedText(transcript);
}, [transcript]);
return (
<div>
<textarea value={editedText} onChange={(e) => setEditedText(e.target.value)} />
<button onClick={listening ? stopListening : startListening}>
{listening ? 'Stop' : 'Speak'}
</button>
</div>
);
};Handling Common Issues
As I worked with speech recognition, I encountered a common issue related to async/await syntax: “ReferenceError: regeneratorRuntime is not defined.” This error occurs when your JavaScript code uses modern features like async/await but the environment doesn’t support them natively. To fix this, I installed the regenerator-runtime package and imported it at the top of my main JavaScript file.
Amazon Basics HDMI Cable, 6ft, 4K@60Hz, High-Speed 4K HDMI 2.0 Cord (18Gbps), 2160p, 48 bit, Compatible with TV/PS5/Xbox/Roku, Black
$4.20 (as of February 12, 2026 04:35 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)etguuds USB to USB C Cable 3ft, 2-Pack USB A to Type C Charger Cord Fast Charging for Samsung Galaxy A17 A16 A15 A26 A36 A56, S25 S24 S23 S22 S21 S20 S10 Note 20, for iPhone 17 16 15, Moto, Gray
$6.99 (as of February 12, 2026 04:35 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)HP 63 Black Ink Cartridge | Works with DeskJet 1112, 2130, 3630; Envy 4510, 4520; OfficeJet 3830, 4650, 5200 Series | Eligible for Instant Ink | F6U62AN
(as of February 12, 2026 04:35 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)npm install regenerator-runtimeimport 'regenerator-runtime/runtime';This ensures that the necessary polyfills are included, allowing async/await to work correctly.
Conclusion
Implementing voice input in my React app was a rewarding experience that significantly enhanced user interaction. By using the react-speech-recognition library, I was able to integrate speech recognition efficiently and effectively. This feature can be a valuable addition to a wide range of applications, from simple text editors to complex interfaces. As you explore these methods, you’ll find that voice input can make your apps more accessible and user-friendly.


