작업 방식

(삭제한 기능) “취소” 버튼을 누르면 입력/변경했던 값을 초깃값으로 되돌리기

const initialValues = useMemo<ClientProfileUpdateValue>(() => {
  if (user?.userType === "client") {
     const client = user as Client;

     return {
        name: client.name ?? "",
        email: client.email ?? "",
        phone: client.phone ?? "",
        profileImage: client.profileImage ?? "",
        serviceType: client.serviceType ?? [],
        livingArea: client.livingArea ?? [],
     };
  }

  return {
     name: "",
     email: "",
     phone: "",
     profileImage: "",
     serviceType: [],
     livingArea: [],
  };
}, [user]);

// ✅ react-hook-form
const {
  register,
  handleSubmit,
  setError,
  setValue,
  watch,
  control,
  formState: { errors, isValid },
  reset,
} = useForm<ClientProfileUpdateValue>({
  mode: "onChange",
  resolver: zodResolver(updateClientProfileSchema(user?.provider)),
  defaultValues: initialValues, // 새로고침 해야 나옴 (취소 버튼은 상관x)
});

// ✅ 기본값 초기화 설정333 = 이름 등을 보존하려면 현재 값 기준으로 써야 함 (취소 버튼으로 연결)
const handleCancel = () => {
  reset({ ...initialValues });
  reset(initialValues);
};

[배운 점] 버튼 type=”reset”으로 설정하면, react-hook-form 초깃값 설정이 안 먹히고 입력창의 전체 값을 지워버린다. 초깃값을 보존해야 하는 경우는 button type="button"으로 설정해야 한다.

문제 및 해결책 : 함수 실행 순서 충돌로 기능이 사라짐

js의 비동기 함수는 “스택” 자료형이라 후입선출로 출력됨. 경로 이동 코드와 겹치면 알림 적용 or 상태 변경을 못할 확률이 있음.

→ 나중에 setTimeout 없이 User 상태 변경 + 알림 + 경로 이동이 되도록 구현해볼 것.

소셜 로그인 시 프로필 수정 페이지에서 비밀번호 유효성 검사 제거

(코드가 똑같아서 BE에서 다룸)