Skip to content

Commit

Permalink
fix: Remove event listener on unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
sonngdev committed Jan 18, 2023
1 parent 128a969 commit 323f16e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ function App() {

// Display voices when they become available
useEffect(() => {
window.speechSynthesis.addEventListener('voiceschanged', () => {
const updateVoiceSettings = () => {
const newVoices = window.speechSynthesis.getVoices();
const defaultVoice = newVoices.find((voice) => voice.default);
setVoices(newVoices);
Expand All @@ -194,7 +194,19 @@ function App() {
if (defaultVoice) {
defaultSettingsRef.current.voiceURI = defaultVoice.voiceURI;
}
});
};

window.speechSynthesis.addEventListener(
'voiceschanged',
updateVoiceSettings,
);

return () => {
window.speechSynthesis.removeEventListener(
'voiceschanged',
updateVoiceSettings,
);
};
}, []);

useEffect(() => {
Expand Down

0 comments on commit 323f16e

Please sign in to comment.