๋šœ๋ถ€๋‹ˆ์˜ Devlog

react-hook-form์„ ์ ์šฉํ•ด๋ณด์ž (feat. ํšŒ์›๊ฐ€์ž…)

by ๋šœ๋ถ€๋‹ˆ

react-hook-form ๋ž€?

react-hook-form ์‚ฌ์šฉ์„ ํ•˜๋ฉด form์„ ๋” ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, ์•„๋ž˜์™€ ๊ฐ™์€ ์žฅ์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

์žฅ์ 

  • ๋ฐฐ์šฐ๊ธฐ ์‰ฝ๋‹ค
  • ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์— ์œ ์šฉํ•˜๋‹ค.
  • HTML ํ‘œ์ค€์„ ํ™œ์šฉํ•˜์—ฌ ์•ˆ์ •์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.
  • ์žฌ ๋ Œ๋”๋ง ํšŸ์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•œ๋‹ค.
    • ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ฆ‰๊ฐ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ ๋‚ด ์š”์†Œ๋“ค์˜ ์˜๋ฏธ ์—†๋Š” ์žฌ ๋ Œ๋”๋ง์„ ๋ง‰์•„์ค€๋‹ค.
    • ์ „์ฒด ์–‘์‹์„ ์žฌ ๋ Œ๋”๋ง ํ•˜์ง€ ์•Š๊ณ , ๊ฐœ๋ณ„ ์ž…๋ ฅ ๋ฐ ์–‘์‹ ์ƒํƒœ ์—…๋ฐ์ดํŠธ๋ฅผ ๊ตฌ๋…ํ•  ์ˆ˜ ์žˆ๋‹ค.

์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋žตํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ https://react-hook-form.com/ ์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

react-hook-form ์„ค์น˜ํ•˜๊ธฐ

์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, react-hook-form์ด ์„ค์น˜๋˜์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

npm install react-hook-form

 

๊ฐ„๋‹จํ•œ ํšŒ์›๊ฐ€์ž… ๋งŒ๋“ค์–ด๋ณด๊ธฐ

๊ฐ„๋‹จํ•œ ํšŒ์›๊ฐ€์ž…์„ ํ†ตํ•ด react-hook-form์„ ์‚ฌ์šฉํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ, css ์ฝ”๋“œ๋Š” ์ƒ๋žต๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

import {useForm} from "react-hook-form";

const Signup = () => {
    const {handleSubmit, register, formState: {errors}} = useForm();

    function handleFormSubmit(values) {
        console.log("values : ", values);
    }

    return (
        <>
            <div id="signupWrapper">
                <div className="title text-2xl">ํšŒ์›๊ฐ€์ž…</div>
                <form className="form-submit" onSubmit={handleSubmit(handleFormSubmit)}>
                    <label>
                        ์ด๋ฉ”์ผ(ID)
                        <input type="email" name="email" {...register("email", {
                            required: "์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.",
                            pattern: {
                                value: /[a-z0-9]{2,}@[a-z0-9-]{2,}\\.[a-z0-9]{2,}/i,
                                message: "์ด๋ฉ”์ผ ํ˜•์‹์— ๋งž๊ฒŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
                            }
                        })}/>
                        <span>{errors?.email?.message}</span>
                    </label>
                    <label>
                        ๋น„๋ฐ€๋ฒˆํ˜ธ(PW)
                        <input type="password" name="password" {...register("password", {
                            required: "๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.",
                            pattern: {
                                value: /^(?=.*[A-Za-z])(?=.*\\d)(?=.*[!@#$%?*])[A-Za-z\\d@$!%*#?&]{8,}$/,
                                message: "์˜๋ฌธ์ž, ์ˆซ์ž ๋ฐ ํŠน์ˆ˜๋ฌธ์ž(!@#$%?*)๋ฅผ ํฌํ•จํ•˜์—ฌ 8์ž๋ฆฌ ์ด์ƒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
                            }
                        })}/>
                        <span>{errors?.password?.message}</span>
                    </label>
                    <label>
                        ์ด๋ฆ„
                        <input type="name" name="name" {...register("name", {
                            required: "์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
                        })}/>
                        <span>{errors?.name?.message}</span>
                    </label>
                    <label>
                        ํ•ธ๋“œํฐ๋ฒˆํ˜ธ
                        <input type="phone" name="phone" {...register("phone", {
                            required: "ํ•ธ๋“œํฐ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.",
                            pattern: {
                                value: /^\\d{3}-\\d{3,4}-\\d{4}$/,
                                message: "000-0000-0000 ํ˜•์‹์œผ๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
                            }
                        })}/>
                        <span>{errors?.phone?.message}</span>
                    </label>
                    <label>
                        ๋‹‰๋„ค์ž„
                        <input type="nickname" name="nickname" {...register("nickname", {
                            required: "๋‹‰๋„ค์ž„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
                        })}/>
                        <span>{errors?.nickname?.message}</span>
                    </label>
                    <div className="daangn-wrapper">
                        <button className="btn-daangn" type="submit">๊ฐ€์ž…ํ•˜๊ธฐ</button>
                    </div>
                </form>
            </div>
        </>
    )
}

export default Signup;

์•„๋ž˜ ์ด๋ฏธ์ง€๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ์„ ์ œ๋Œ€๋กœ ํ•˜๋ฉด ๋‹‰๋„ค์ž„ ์ž…๋ ฅ์ฐฝ์ฒ˜๋Ÿผ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋œจ์ง€ ์•Š๊ณ , ํ•„์ˆ˜ ์ž…๋ ฅ์„ ์ฑ„์šฐ์ง€ ์•Š๊ฑฐ๋‚˜ ์œ ํšจํ•˜์ง€ ์•Š์€ ๋ฐฉ์‹์œผ๋กœ ์ž…๋ ฅํ•˜๋ฉด ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋œจ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฐธ๊ณ 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

๋šœ๋ถ€๋‹ˆ์˜ Devlog

๋šœ๋ถ€๋‹ˆ

ํ™œ๋™ํ•˜๊ธฐ