λ¬Έμ μ : μ΄λ©μΌμ μμ±νλ€κ° μ§μ°λ©΄ μ¬μ© κ°λ₯ν μ΄λ©μΌ μ
λλ€.
λ¬Έκ΅¬κ° μ¬λΌμ§μ§ μλλ€.
μ½λμ μΌλΆ
// signup.tsx
const Signup: NextPage = () => {
...
const [emailValid, setEmailValid] = useState('');
const handleBlur = async () => {
const res = await axios(`${API_ENDPOINT}/user/emailvalid`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
data: JSON.stringify({
user: {
email: getValues().email,
},
}),
});
setEmailValid(res.data.message);
};
const handleChange = () => {
setEmailValid('');
};
...
return (
...
<BoxInp>
<Label htmlFor="email">
μ΄λ©μΌ
<Input
type="email"
id="email"
placeholder="μ΄λ©μΌ μ£Όμλ₯Ό μ
λ ₯ν΄ μ£ΌμΈμ."
required
{...register('email', {
required: true,
pattern: regExpEm,
onBlur: handleBlur,
onChange: handleChange,
})}
/>
</Label>
{errors?.email?.type === 'required' && (
<TxtError>* μ΄λ©μΌμ μ
λ ₯ν΄μ£ΌμΈμ</TxtError>
)}
{errors?.email?.type === 'pattern' && (
<TxtError>* μλͺ»λ μ΄λ©μΌ νμμ
λλ€.</TxtError>
)}
{emailValid === 'μ¬μ© κ°λ₯ν μ΄λ©μΌ μ
λλ€.' && (
<TxtSuccess>* μ¬μ© κ°λ₯ν μ΄λ©μΌ μ
λλ€.</TxtSuccess>
)}
{emailValid === 'μ΄λ―Έ κ°μ
λ μ΄λ©μΌ μ£Όμ μ
λλ€.' && (
<TxtError>* μ΄λ―Έ κ°μ
λ μ΄λ©μΌ μ£Όμμ
λλ€.</TxtError>
)}
</BoxInp>
...
);
};
export default Signup;
handleBlur
λ΄μ early return
μ¬μ©const handleBlur = async () => {
// λ°νκ° μμ΄ ν¨μκ° μ’
λ£ (early return)
if (errors?.email?.type === 'pattern') return;
const res = await axios(`${API_ENDPOINT}/user/emailvalid`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
data: JSON.stringify({
user: {
email: getValues().email,
},
}),
});
setEmailValid(res.data.message);
};
ν¨μ λ΄μ else
λ¬Έμ μλ₯Ό 0μΌλ‘ μ€μΌ μ μλ€.
early return
μ λ³Έμ§μ ν¨μ λ΄μ else
λ¬Έμ if
λ¬Έμ return
ν€μλλ‘ λ체νλ κ²
νΉμ 쑰건 νμ λ€λ₯΄κ² λμνλ ν¨μ μμ
function willItBlend(someObject) {
let itWillBlend;
if (typeof someObject === 'object') {
if (someObject.blendable === 'It will blend') {
itWillBlend = true;
} else {
itWillBlend = false;
}
} else {
itWillBlend = false;
}
return itWillBlend;
}
early return
ν¨ν΄μΌλ‘ μ½λ μμ±else
λ¬Έ μ κ±°function willItBlend(someObject) {
if (typeof someObject !== 'object') {
return false;
}
if (someObject.blendable === 'It will blend') {
return false;
}
return true;
}
리ν©ν λ§_1 : νλμ
if
λ¬Έμ 리ν©ν λ§function willItBlend(someObject) { if (typeof someObject !== 'object' || someObject.blendable === 'It will blend') { return false } return true; }
리ν©ν λ§_2 : μΌνμ°μ°μλ₯Ό μ΄μ©ν 리ν©ν λ§
function willItBlend(someObject) { return typeof someObject !== 'object' || someObject.blendable === 'It will blend' ? false : true }
early return
: ν¨μμ λλ¨Έμ§κ° μ€νλ λκΉμ§ κΈ°λ€λ¦¬μ§ μκ³ , 쑰건문μ κ²°κ³Όλ₯Ό μ¦μ λ°νν μ μλλ‘ νλ€.early return
μ μ¬μ©μ΄ κ°λ
μ±μ λμ΄λ λ°©λ²μΈκ°?if/else
λ¬Έμ μ¬μ©λ³΄λ€ μ¬λ¬ κ°μ if
λ¬Έκ³Ό return
ν€μλμ μ¬μ©μ ν λμ μ΄ν΄νκΈ° μ΄λ ΅κ² λκ»΄μ§κΈ°λ νλ€. single exit function
λ³΄λ€ μΌλ§λ μ±λ₯μ΄ μ’μκ°?
getValues: (payload?: string | string[]) => Object
- νΌμ κ°(value)λ₯Ό μ½μ λ μ¬μ©
watch
μ μ°¨μ΄μ :getValues
λ νΈλ¦¬κ±°λ₯Ό 리λ λλ§νκ±°λinput
κ°μ λ³νλ₯Ό subscribe νμ§ μλλ€.
getValues()
: νΌμ μ 체 κ°μ μ½μgetValues('inputName')
: ν΄λΉname
μμ±μ κ°μ μ½μgetValues(['inputName1', 'inputName2'])
: μ§μ λ μ¬λ¬input
μ κ°μ μ½μimport React from "react"; import { useForm } from "react-hook-form"; type FormInputs = { test: string test1: string } export default function App() { const { register, getValues } = useForm<FormInputs>(); return ( <form> <input {...register("inputName1")} /> <input {...register("inputName2")} /> <button type="button" onClick={() => { const values = getValues(); // { inputName1: "inputName1-inputValue", inputName2: "inputName2-inputValue" } const singleValue = getValues("inputName1"); // "inputName1-inputValue" const multipleValues = getValues(["inputName1", "inputName2"]); // ["inputName1-inputValue", "inputName2-inputValue"] }} Get Values </button> </form> ); }
import styled from '@emotion/styled';
import axios from 'axios';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { API_ENDPOINT, BORDER, BUTTON, COLORS } from '../../constants';
export const SignupEmail = ({
setPages,
setUserInfo,
}: {
setPages: (value: boolean) => void;
setUserInfo: (value: { email: string; password: string }) => void;
}) => {
const regExpEm =
/^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/i;
const regExgPw = /^[A-Za-z0-9]{6,12}$/;
const [emailValid, setEmailValid] = useState('');
const {
register,
getValues,
formState: { errors, isValid },
} = useForm<{ email: string; password: string }>({ mode: 'onChange' });
const handleBlur = async () => {
if (errors?.email?.type === 'pattern') return;
const res = await axios(`${API_ENDPOINT}/user/emailvalid`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
data: JSON.stringify({
user: {
email: getValues().email,
},
}),
});
setEmailValid(res.data.message);
};
const handleChange = () => {
setEmailValid('');
};
const goToSetProfile = () => {
setPages(false);
setUserInfo({
email: getValues().email,
password: getValues().password,
});
};
return (
<MainSignup>
<TitleMain>μ΄λ©μΌλ‘ νμκ°μ
</TitleMain>
<FormLogin>
<BoxInp>
<Label htmlFor="email">
μ΄λ©μΌ
<Input
type="email"
id="email"
placeholder="μ΄λ©μΌ μ£Όμλ₯Ό μ
λ ₯ν΄ μ£ΌμΈμ."
required
{...register('email', {
required: true,
pattern: regExpEm,
onBlur: handleBlur,
onChange: handleChange,
})}
/>
</Label>
{errors?.email?.type === 'required' && (
<TxtError>* μ΄λ©μΌμ μ
λ ₯ν΄μ£ΌμΈμ</TxtError>
)}
{errors?.email?.type === 'pattern' && (
<TxtError>* μλͺ»λ μ΄λ©μΌ νμμ
λλ€.</TxtError>
)}
{emailValid === 'μ¬μ© κ°λ₯ν μ΄λ©μΌ μ
λλ€.' && (
<TxtSuccess>* μ¬μ© κ°λ₯ν μ΄λ©μΌ μ
λλ€.</TxtSuccess>
)}
{emailValid === 'μ΄λ―Έ κ°μ
λ μ΄λ©μΌ μ£Όμ μ
λλ€.' && (
<TxtError>* μ΄λ―Έ κ°μ
λ μ΄λ©μΌ μ£Όμμ
λλ€.</TxtError>
)}
</BoxInp>
<BoxInp>
<Label htmlFor="password">
λΉλ°λ²νΈ
<Input
type="password"
id="password"
placeholder="λΉλ°λ²νΈλ₯Ό μ€μ ν΄ μ£ΌμΈμ."
required
{...register('password', { required: true, pattern: regExgPw })}
/>
</Label>
{errors?.password?.type === 'pattern' && (
<TxtError>
* λΉλ°λ²νΈλ μλ¬Έ(λ/μλ¬Έμ ꡬλΆ), μ«μ μ‘°ν©νμ¬ 6~12μλ¦¬λ‘ μ
λ ₯ν΄
μ£ΌμΈμ.
</TxtError>
)}
</BoxInp>
<BtnNext
type="button"
onClick={goToSetProfile}
disabled={!isValid || emailValid !== 'μ¬μ© κ°λ₯ν μ΄λ©μΌ μ
λλ€.'}
>
λ€μ
</BtnNext>
</FormLogin>
</MainSignup>
);
};
μ°μ λ무 μ κΈ°ν΄μ~