我有监听器,当音量变化时,执行回调函数4次,调用其他函数,我只需要执行一个函数,最好的方式是什么?
当lisiner检测到任何更改时,回调调用recognitionCameraBySoundVolum() 4次
volumeListener = SystemSetting.addVolumeListener((data) => {
recognitionCameraBySoundVolum();
});只需要导出此函数一次而不是4次。
const recognisionCameraBySoundVolum = () => {
console.log("hi from recognitionCameraBySoundVolum ");
}发布于 2022-07-15 05:27:15
你用过useEffect钩吗?
应该是这样的。
useEffect(() => {
volumeListener = SystemSetting.addVolumeListener((data) => {
recognitionCameraBySoundVolum();
});
// return remove listener method in the cleanup section
}, []);发布于 2022-07-15 08:07:15
这对我起作用了
useEffect(() => {
const volumeListener = SystemSetting.addVolumeListener( (data) => {
const volume = data.value;
});
return () =>
SystemSetting.removeVolumeListener(volumeListener)
}, [])https://stackoverflow.com/questions/72989044
复制相似问题