From a142112d6c15e678e0398afb8e236daeba69abdb Mon Sep 17 00:00:00 2001 From: DimaDemchenko Date: Wed, 17 Jan 2024 13:53:54 +0200 Subject: [PATCH] fixes after code review --- src/components/camera-stream/CameraStream.tsx | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/components/camera-stream/CameraStream.tsx b/src/components/camera-stream/CameraStream.tsx index 2d6bcbe..889b1d3 100644 --- a/src/components/camera-stream/CameraStream.tsx +++ b/src/components/camera-stream/CameraStream.tsx @@ -3,31 +3,31 @@ import styles from './CameraStream.module.css' export const CameraStream = () => { const videoRef = useRef(null) - const [showCam, setShowCam] = useState(false) + const [isVideoVisible, setIsVideoVisible] = useState(false) - const getVideo = async () => { - try { - const stream = await navigator.mediaDevices.getUserMedia({ - video: { width: 1280, height: 720 }, - }) + useEffect(() => { + const initializeCamera = async () => { + try { + const stream = await navigator.mediaDevices.getUserMedia({ + video: { width: 1280, height: 720 }, + }) - const video = videoRef.current + const video = videoRef.current - if (!video) return + if (!video) return - video.srcObject = stream - video.play() - } catch (error) { - console.log(error) + video.srcObject = stream + video.play() + } catch (error) { + console.log(error) + } } - } - useEffect(() => { - getVideo() + initializeCamera() }, []) const handleChange = () => { - setShowCam(!showCam) + setIsVideoVisible(!isVideoVisible) } return ( @@ -36,14 +36,14 @@ export const CameraStream = () => { + />