Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent auto resetTranscript on listening when default value is set #65

Closed
nirmaldymensions opened this issue Sep 19, 2020 · 1 comment

Comments

@nirmaldymensions
Copy link

nirmaldymensions commented Sep 19, 2020

I've used this hook in a form where a textarea value gets updated with the spoken text. Works perfectly fine when the textarea is empty. However, if I have a default value set to the textarea, it gets reset on listening, which I'm guessing is the normal behaviour. But I don't want that. For eg. if I have "Default Notes Value" as the default value of the textarea and I start listening, the new content should append to the already present one and not replace it.

Here's a working example - https://codesandbox.io/s/adoring-wiles-q4tnf

I did try setting the default value state as the transcript onMount but it says transcipt is not a function:

useEffect(() => {
  notes && transcript(notes)
}, [])
@JamesBrill
Copy link
Owner

JamesBrill commented Oct 11, 2020

Hi @nirmaldymensions

The transcript string emitted by the Web Speech API cannot be modified directly, except by calling resetTranscript. If you want to display any additional text besides the transcript, you will need to manage that state yourself and concatenate the two. I've made some minor modifications to your example to demonstrate how you might do this: https://codesandbox.io/s/serene-feather-hl07f?file=/src/TextareaWithSpeech.js

Note that it was not the transcript that was being reset when you first started the Web Speech API, just your custom notes state. You were assigning the value of the transcript (initially an empty string) to your notes without storing the original notes value in its own state. As these are independent of each other, only the transcript would be displayed after the first update, and any default value that was there before would be wiped out. Hence the need to maintain your own state and combine the two in some way.

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants