reactjs - 如何在 React Native 中删除 react-native-tts 异步函数的事件监听器
问题描述
我尝试了以下代码来删除componentWillUnmount
生命周期方法中的事件侦听器:
class MyComponent extends Component {
constructor(props) {
super(props);
this.subscription = Tts.addEventListener('tts-finish', this._startRecognition.bind(this));
}
componentWillMount() {
Tts.speak('Welcome to Hello World');
}
_startRecognition() {
console.log('==================== recognizing text that was spoken ====================');
}
componentWillUnmount() {
Tts.removeEventListener('tts-finish');
}
}
当我再次打开此组件时,它将启动该_startRecognition
方法。有没有一种优雅的方法来删除事件监听器。任何帮助将不胜感激。
解决方案
你可以试试下面的吗?
class MyComponent extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
Tts.speak('Welcome to Hello World');
Tts.addEventListener('tts-finish', this._startRecognition)
}
_startRecognition() {
console.log('==================== recognizing text that was spoken ====================');
}
componentWillUnmount() {
Tts.removeEventListener('tts-finish', this._startRecognition);
}
}
推荐阅读
- angular - SignalR错误:无法启动连接:错误:客户端支持的传输均不受服务器支持
- java - 在网格中查找某些多米诺骨牌的所有位置
- kubernetes - 如何在 GKE 中正确执行 cron 任务
- nginx - 如何在 Nginx 反向代理后面集成 Minio 并与 JavaScript API 交互?
- python - Python Telegram 机器人获得所有工作和他们的名字?
- c# - 将 MenuItems 添加到控制台应用程序中 TrayIcon 的上下文菜单
- c# - restsharp 响应不包含任何内容
- haskell - 在使用使用记录语法定义的构造函数时如何定义 Applicative 和 Monad 的实例
- swift - 如何为 Publisher 编写自定义运算符?
- mysql - 如何在 laravel 中制作像下面这样的连接表