Skip to content

Commit

Permalink
Feat: #80 프로필 이미지 URL 해제 코드 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoonyesol committed Aug 26, 2024
1 parent b0ecc30 commit 9966f54
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/components/user/auth-form/ProfileImageContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useFormContext } from 'react-hook-form';
import { convertBytesToString } from '@utils/converter';
import { USER_SETTINGS } from '@constants/settings';
import useToast from '@hooks/useToast';
import { useEffect, useRef } from 'react';

type ProfileImageContainerProps = {
imageUrl: string;
Expand All @@ -13,8 +14,15 @@ type ProfileImageContainerProps = {
export default function ProfileImageContainer({ imageUrl, setImageUrl }: ProfileImageContainerProps) {
const { setValue } = useFormContext();
const { toastWarn } = useToast();
const imageRef = useRef<string | null>(null);

// 컴포넌트 언마운트 시 이미지 URL 해제
useEffect(() => {
return () => {
if (imageRef.current) URL.revokeObjectURL(imageRef.current);
};
}, []);

// 이미지 관련 코드
const handleChangeImg = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];

Expand All @@ -29,9 +37,15 @@ export default function ProfileImageContainer({ imageUrl, setImageUrl }: Profile
const image = URL.createObjectURL(file);
setImageUrl(image);
setValue('profileUrl', image);
imageRef.current = image;
};

const handleRemoveImg = () => {
if (imageRef.current) {
URL.revokeObjectURL(imageRef.current);
imageRef.current = null;
}

setImageUrl('');
setValue('profileUrl', '');
};
Expand Down

0 comments on commit 9966f54

Please sign in to comment.