@@ -6,6 +6,7 @@ function useCamera(setPhoto: (photo: string | null) => void) {
66 const videoRef = useRef < HTMLVideoElement > ( null )
77 const canvasRef = useRef < HTMLCanvasElement > ( null )
88 const streamRef = useRef < MediaStream | null > ( null )
9+ const isMountedRef = useRef ( true )
910
1011 const openCamera = useCallback ( async ( ) => {
1112 if ( ! navigator . mediaDevices || ! navigator . mediaDevices . getUserMedia ) {
@@ -27,14 +28,32 @@ function useCamera(setPhoto: (photo: string | null) => void) {
2728 }
2829
2930 const stream = await navigator . mediaDevices . getUserMedia ( constraints )
31+
32+ if ( ! isMountedRef . current ) {
33+ stream . getTracks ( ) . forEach ( ( track ) => track . stop ( ) )
34+ return
35+ }
36+
3037 streamRef . current = stream
3138
32- if ( videoRef . current ) {
39+ if ( videoRef . current && isMountedRef . current ) {
3340 videoRef . current . srcObject = stream
34- await videoRef . current . play ( )
41+ try {
42+ await videoRef . current . play ( )
43+ } catch ( playError ) {
44+ if ( playError instanceof Error && playError . name !== 'AbortError' ) {
45+ console . error ( '비디오 재생 중 오류 발생:' , playError )
46+ }
47+ }
3548 }
3649 } catch ( err ) {
37- console . error ( '카메라를 열 수 없습니다:' , err )
50+ if (
51+ isMountedRef . current &&
52+ err instanceof Error &&
53+ err . name !== 'AbortError'
54+ ) {
55+ console . error ( '카메라를 열 수 없습니다:' , err )
56+ }
3857 }
3958 } , [ isRearCamera ] )
4059
@@ -103,8 +122,10 @@ function useCamera(setPhoto: (photo: string | null) => void) {
103122 } , [ ] )
104123
105124 useEffect ( ( ) => {
125+ isMountedRef . current = true
106126 openCamera ( )
107127 return ( ) => {
128+ isMountedRef . current = false
108129 if ( streamRef . current ) {
109130 streamRef . current . getTracks ( ) . forEach ( ( track ) => track . stop ( ) )
110131 }
0 commit comments