voice
voice copied to clipboard
Cannot update listeners while Voice is recording.
trafficstars
The current API doesn't allow to update listeners after Voice.start() while it's recording. This can be problematic if you're using hooks and your callback functions need to update as your component state is changing.
I've written the following patch for anyone interested, to be applied with patch-package:
diff --git a/node_modules/@react-native-community/voice/dist/index.js b/node_modules/@react-native-community/voice/dist/index.js
index a10af13..6852f11 100644
--- a/node_modules/@react-native-community/voice/dist/index.js
+++ b/node_modules/@react-native-community/voice/dist/index.js
@@ -52,7 +52,7 @@ class RCTVoice {
}
start(locale, options = {}) {
if (!this._loaded && !this._listeners && voiceEmitter !== null) {
- this._listeners = Object.keys(this._events).map((key) => voiceEmitter.addListener(key, this._events[key]));
+ this._listeners = Object.keys(this._events).map((key) => voiceEmitter.addListener(key, (event) => {return this._events[key](event);}));
}
return new Promise((resolve, reject) => {
const callback = (error) => {`