Skip to content

Commit

Permalink
feat: disable camera on unmount component
Browse files Browse the repository at this point in the history
  • Loading branch information
paulovitin committed Dec 10, 2023
1 parent 5c31d4d commit 83133fc
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,17 @@
function QRReader({ onCode }) {
const canvasEl = React.useRef(null);
const videoEl = React.useRef(null);
const streamRef = React.useRef(null);

React.useLayoutEffect(() => {
React.useEffect(() => {
if (!videoEl.current || !canvasEl.current) {
return;
}

navigator.mediaDevices
.getUserMedia({ video: { facingMode: "environment" } })
.then((stream) => {
streamRef.current = stream;
let video = videoEl.current;
video.srcObject = stream;
video.setAttribute("playsinline", true);
Expand All @@ -197,6 +199,10 @@
tick(videoEl.current, canvasEl.current, onCode)
);
});

return () => {
streamRef.current?.getTracks().forEach((track) => track.stop());
};
}, [videoEl, canvasEl]);

return (
Expand Down

0 comments on commit 83133fc

Please sign in to comment.