Web Speech API SpeechRecognition not defined when using React.js

10.5k views Asked by At

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?

2

There are 2 answers

2
piouson On

try window.SpeechRecognition || window.webkitSpeechRecognition;

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

0
tsonga absolutely On

Try this:

Instead of

const recognition = new SpeechRecognition();

Write:

const recognition = new speechRecognition();

This helped me!