Skip to content

Commit

Permalink
Merge pull request #13 from Intentface/fix-hydration-problem
Browse files Browse the repository at this point in the history
Fix hydration error regarding isSupported flag
  • Loading branch information
latenssi authored Sep 12, 2024
2 parents fd5b53d + 4da06da commit 383462f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-plants-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@intentface/react-speech-recognition": patch
---

Fix hydration error regarding isSupported flag
15 changes: 10 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function useSpeechRecognition({
const [interimTranscript, setInterimTranscript] = useState("");
const [isFinal, setIsFinal] = useState(false);
const [isListening, setIsListening] = useState(false);
const [isSupported, setIsSupported] = useState(false);
const [error, setError] = useState<SpeechRecognitionErrorEvent | null>(null);

const recognitionRef = useRef<SpeechRecognition | null>(null);
Expand All @@ -47,6 +48,10 @@ export function useSpeechRecognition({
return window.SpeechRecognition || window.webkitSpeechRecognition;
}, []);

useEffect(() => {
setIsSupported(!!SpeechRecognition);
}, [SpeechRecognition]);

useEffect(() => {
if (!SpeechRecognition) return;

Expand Down Expand Up @@ -110,14 +115,14 @@ export function useSpeechRecognition({
}, [SpeechRecognition, continuous, lang, timeout]);

useEffect(() => {
if (!SpeechRecognition) return;
if (!isSupported) return;
if (onUpdate) onUpdate({ transcript, interimTranscript, isFinal });
}, [onUpdate, transcript, interimTranscript, isFinal, SpeechRecognition]);
}, [onUpdate, transcript, interimTranscript, isFinal, isSupported]);

useEffect(() => {
if (!SpeechRecognition) return;
if (!isSupported) return;
if (onError) onError({ error });
}, [onError, error, SpeechRecognition]);
}, [onError, error, isSupported]);

const start = useCallback(() => {
if (!recognitionRef.current) return;
Expand All @@ -134,7 +139,7 @@ export function useSpeechRecognition({
interimTranscript,
isListening,
isFinal,
isSupported: !!SpeechRecognition,
isSupported,
start,
stop,
error,
Expand Down

0 comments on commit 383462f

Please sign in to comment.