Skip to content

Commit

Permalink
♻️ watch 메서드를 useWatch로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
shimdokite committed Apr 22, 2024
1 parent e131053 commit f29875c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 27 deletions.
6 changes: 3 additions & 3 deletions client/src/components/sign/SignPasswordInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import getPasswordByType from '@/utils/getPasswordByType';
interface SignPasswordInputProps {
tag: 'password' | 'passwordCheck';
register: UseFormRegister<SignFormValue>;
watch: UseFormWatch<SignFormValue>;
password: string;
errors: FieldErrors<SignFormValue>;
disabled?: boolean;
}

export default function SignPasswordInput({
tag,
register,
watch,
password,
errors,
disabled,
}: SignPasswordInputProps) {
const passwordFormat = getPasswordByType(tag, watch);
const passwordFormat = getPasswordByType(tag, password);

const errorMsg = errors[tag]?.message;

Expand Down
10 changes: 5 additions & 5 deletions client/src/components/signin/SigninForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useForm } from 'react-hook-form';
import { useForm, useWatch } from 'react-hook-form';

import useModalStore from '@/stores/modalStore';

Expand All @@ -16,15 +16,15 @@ export default function SigninForm() {
register,
handleSubmit,
formState: { errors, isSubmitting },
watch,
control,
} = useForm<SignFormValue>();

const { open, changeType } = useModalStore();

const { mutate: onSiginIn } = useSigninMutation();

const email = watch('email');
const password = watch('password');
const email = useWatch({ name: 'email', control });
const password = useWatch({ name: 'password', control });

return (
<section className="flex flex-col items-center gap-5 px-5 mt-3">
Expand All @@ -37,7 +37,7 @@ export default function SigninForm() {
tag="password"
register={register}
errors={errors}
watch={watch}
password={password}
/>
</div>

Expand Down
16 changes: 8 additions & 8 deletions client/src/components/signup/SignupForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useForm } from 'react-hook-form';
import { useForm, useWatch } from 'react-hook-form';

import useModalStore from '@/stores/modalStore';
import useSignStore from '@/stores/signStore';
Expand All @@ -20,20 +20,20 @@ export default function SignupForm() {
register,
handleSubmit,
formState: { errors, isSubmitting },
watch,
control,
} = useForm<SignFormValue>();

const email = useWatch({ name: 'email', control });
const password = useWatch({ name: 'password', control });
const nickname = useWatch({ name: 'nickname', control });

const { changeType } = useModalStore();
const { isCode, setIsCode } = useSignStore();
const { isGuestMode, setClear } = useUserStore();

const { mutate } = useAuthEmailMutation();
const { mutate: onSignup } = useSignupMutation();

const email = watch('email');
const password = watch('password');
const nickname = watch('nickname');

const onValidateEmail = () => {
if (!email) return;

Expand Down Expand Up @@ -85,15 +85,15 @@ export default function SignupForm() {
tag="password"
register={register}
errors={errors}
watch={watch}
password={password}
disabled={!isCode}
/>

<SignPasswordInput
tag="passwordCheck"
register={register}
errors={errors}
watch={watch}
password={password}
disabled={!isCode}
/>

Expand Down
14 changes: 3 additions & 11 deletions client/src/utils/getPasswordByType.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { UseFormWatch } from 'react-hook-form';

import { SignFormValue } from '@/types/common';

import { SIGN_VAILDATION } from '@/constants/contents';

export default function getPasswordByType(
tag: string,
watch?: UseFormWatch<SignFormValue>,
) {
export default function getPasswordByType(tag: string, password?: string) {
if (tag === 'password') {
return {
validation: {
Expand All @@ -20,12 +13,11 @@ export default function getPasswordByType(
};
}

if (tag === 'passwordCheck' && watch) {
if (tag === 'passwordCheck' && password) {
return {
validation: {
required: true,
validate: (value: string) =>
value === watch('password') || SIGN_VAILDATION[tag],
validate: (value: string) => value === password || SIGN_VAILDATION[tag],
},
};
}
Expand Down

0 comments on commit f29875c

Please sign in to comment.