Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Speech API SpeechRecognition not defined when using React.js

I am using React.js along with the Web Speech API's SpeechRecognition, however, it does not work and I get the error "ReferenceError: SpeechRecognition is not defined." The code I am using is directly from the SpeechRecognition documentation:

const SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
const recognition = new SpeechRecognition();

The first line causes the error, but without it, the second line will cause the same error. How can I fix this?

like image 461
Anchey Peng Avatar asked Aug 01 '26 04:08

Anchey Peng


1 Answers

try window.SpeechRecognition || window.webkitSpeechRecognition;

See Using the Web Speech API for further explanation why you need the window. prefix.

like image 79
piouson Avatar answered Aug 02 '26 18:08

piouson