我的相机动作异常。当我拍照时,它将立即关闭。但是,当我卸下组件时,即使正在调用完全相同的功能,也可能需要10秒钟才能关闭相机。由于“相机灯”仍处于打开状态,因此我认为它仍处于打开状态。这是背后的逻辑:
class Webcam extends React.Component<Props> {
...
videoRef: any = React.createRef();
componentWillUnmount() {
this.destroyCam();
}
destroyCam() {
this.videoRef.srcObject.getTracks().forEach((track: MediaStream) => {
track.stop();
});
}
onShoot = () => {
var timer = setInterval(() => {
this.countdown--;
if (this.countdown === 0) {
this.destroyCam();
clearInterval(timer);
}
}, 1000);
};
render() {
return(
...
<video ref={el => (this.videoRef = el)}/>
)
}
}
就像我说的那样,当onShoot被触发时,它会立即关闭相机。为什么在卸载组件时不显示?