๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Framework/React

React Hook Form ํšŒ์›๊ฐ€์ž… ์œ ํšจ์„ฑ ์ฒดํฌ

by cariño 2022. 10. 24.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ React Hook Form

 

๋ชจ๋“ˆ ๋‹ค์šด ๋ฐ›๊ธฐ
npm install react-hook-form --save

 

useForm importํ•˜๊ธฐ
import { UseForm } from 'react-hook-form'

 


register()

const { register } = useForm();
{...register("email")}

input ํƒœ๊ทธ ์•ˆ์— register ํ•จ์ˆ˜๋ฅผ spreadํ•ด์„œ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค (ES6 ๋ฌธ๋ฒ•)

 

watch()

const { watch } = useForm();
console.log(watch('email'))`

watch()

์ง€์ •๋œ input์„ ๊ด€์ฐฐํ•˜๊ณ  ํ•ด๋‹น ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ด€์ฐฐ ํ•˜๋ ค๋Š” input ์š”์†Œ์— register๋ฅผ ๋“ฑ๋กํ•ด์ค€๋‹ค.

*๋ฒ„์ „ 7๋ถ€ํ„ฐ ๋ณ€๊ฒฝ๋จ : ref={register} => {...register("email")}


์ฝ˜์†”์„ ํ™•์ธํ•˜๋ฉด ์ž…๋ ฅ์‹œํ‚จ input์— ํƒ€์ดํ•‘๋˜๋Š” ๊ฐ’์ด watch๋กœ ํ‘œ๊ธฐ๊ฐ€ ๋˜๋Š”๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

handleSubmit()

const { handleSubmit } = useForm();
const onValid = (data) => {
        console.log(data);
    }
<form onSubmit={handleSubmit(onValid)}>
...

handleSubmit์€ React Hook Form์—์„œ Submit์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“  ํ•จ์ˆ˜์ด๋‹ค.
react hook form์ด ๋ชจ๋“  validation์„ ๋งˆ์ณค์„๋•Œ ํ˜ธ์ถœ๋œ๋‹ค.

handleSubmit์€ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›์œผ๋ฉฐ ๊ทธ ํ•จ์ˆ˜์— data๋ผ๋Š” ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค. ํ•œ๋ฒˆ์— ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด์„œ ํŽธ๋ฆฌํ•˜๋‹ค.

 

 


validation ์ฒดํฌํ•˜๊ธฐ

required๋Š” inputํƒœ๊ทธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์†์„ฑ์ด๋‹ค. ํ•„์ˆ˜ ์ž…๋ ฅ ์‚ฌํ•ญ์—๋Š” required์†์„ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

์œ ํšจ์„ฑ ์ฒดํฌ์˜ ์กฐ๊ฑด ๋„ฃ๊ธฐ

<input
type="text"
id="email"
{...register("email",
    {
    required: true,
    pattern: {
            value: /\S+@\S+\.\S+/,
            message: "์ด๋ฉ”์ผ ํ˜•์‹์— ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค."}
    	})
 	 } 
 />

[์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์†์„ฑ]
required : ๋ฐ˜๋“œ์‹œ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š”์ง€ ์ฒดํฌ
min : ์ž…๋ ฅ์— ํ—ˆ์šฉ๋˜๋Š” ์ตœ์†Œ ๊ฐ’
max : ์ž…๋ ฅ์— ํ—ˆ์šฉ๋˜๋Š” ์ตœ๋Œ€ ๊ฐ’
minLength : ์ž…๋ ฅ์— ํ—ˆ์šฉ๋˜๋Š” ์ตœ์†Œ ๊ธธ์ด
maxLength : ์ž…๋ ฅ์— ํ—ˆ์šฉ๋˜๋Š” ์ตœ๋Œ€ ๊ธธ์ด
pattern : ์ž…๋ ฅ์— ๋Œ€ํ•œ ์ •๊ทœ์‹ ํŒจํ„ด
validate: ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ๊ฒ€์ฆ

 

์œ ํšจ์„ฑ ์ฒดํฌ ์‹œ ์—๋Ÿฌ๋ฌธ๊ตฌ ์„ค์ •

const { formState: { errors } } = useForm();
return (
    <div>
        <form onSubmit={handleSubmit(onSubmit)}>
            <label htmlFor="email">์ด๋ฉ”์ผ</label>
            <input
                ...
            {errors.email && errors.email.type === "required" && <p>this email field is required</p>}
            {errors.email && errors.email.type === "pattern" && <p>์˜ฌ๋ฐ”๋ฅธ ์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•˜์„ธ์š”</p>}

errors.email: error.email์˜ ์œ ํšจ์„ฑ ์ฒดํฌ์— ๊ฑธ๋ ธ๋‹ค๋ฉด? : erros ๋ Œ๋”๋ง ํ•ด์ฃผ๊ธฐ
errors.email.type === "required": type์ด required์ธ error.email

 

 

ํ˜น์€ ์ด๋ฅด์ผ€ ํ•  ์‘ค์ด์‘ด

 

                        <input type="text"
                            className="useremail"
                            placeholder="์ด๋ฉ”์ผ"
                            {...register("email", {
                                required: true,
                                pattern: {
                                    value: /\S+@\S+\.\S+/,
                                    message: '์˜ฌ๋ฐ”๋ฅธ ํ˜•์‹์„ ์ž…๋ ฅํ•˜์„ธ์š”.'
                                }
                            })}
                        />
                        {errors.email?.type === "required" && <p>์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.</p>}
                        {errors.email?.type === "pattern" && errors.email.message}

<span>{errors?.Email?.message}</span> :? : undefined์—ฌ๋„ ์‹คํ–‰ํ•˜๊ธฐ (not required)

 

์ด๋ฉ”์ผ ์•ˆ์— ์•„๋ฌด๊ฒƒ๋„ ์—†์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ?๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
์ œ์ถœํ•˜๊ณ  ์—๋Ÿฌ๊ฐ€ ์žˆ์„ ๋–ผ react-hook์ด ๊ฒ€์‚ฌ๋ฅผ ํ†ตํ•ด ์—๋Ÿฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์—๋Ÿฌ ๋ฉ”์„ธ์ง€๋Š”, ๋จผ์ € ๊ฐ์ฒด์•ˆ์— ๋„ฃ์–ด๋†“๊ณ  &&์—ฐ์‚ฐ์ž ๋’ค์— message๋กœ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. 

 

 

 

ErrorMessage ์ปดํฌ๋„ŒํŠธ

npm install @hookform/error-message
์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์— ErrorMessage๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  <label>gender</label>
  <select name="gender" id="gender"
      {...register("gender", {
          required: true
      })}
  >
      <option value="">select...</option>
      <option value="male">male</option>
      <option value="female">female</option>
  </select>

  <ErrorMessage
      errors={errors}
      name="gender"
      render={({ message }) => <p>{message} ์„ฑ๋ณ„์„ ์„ ํƒํ•˜์„ธ์š”</p>}
  />

 


 

์ „์ฒด ์ฝ”๋“œ ํ™•์ธํ•˜๊ธฐ

import { useForm } from 'react-hook-form'
import { ErrorMessage } from '@hookform/error-message'

export default function Signup2() {


    const { register, formState: { errors }, handleSubmit } = useForm();
    const onSubmit = data => console.log(data.required);

    // ํ•„์ˆ˜์ž…๋ ฅ์‚ฌํ•ญ์—๋Š” required์†์„ฑ์„ ์‚ฌ์šฉ
    return (
        <div>
            <form onSubmit={handleSubmit(onSubmit)}>
                <label htmlFor="email">์ด๋ฉ”์ผ</label>
                <input
                    type="text"
                    id="email"
                    {...register("email",
                        {
                            required: true,
                            pattern: /\S+@\S+\.\S+/
                            // pattern: {
                            //     value: /\S+@\S+\.\S+/,
                            //     message: "์ด๋ฉ”์ผ ํ˜•์‹์— ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค.",
                            // },
                        })
                    } />
                {errors.email && errors.email.type === "required" && <p>this email field is required</p>}
                {errors.email && errors.email.type === "pattern" && <p>์˜ฌ๋ฐ”๋ฅธ ์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•˜์„ธ์š”</p>}
                {/* email์˜ ์œ ํšจ์„ฑ ์ฒดํฌ์— ๊ฑธ๋ ธ๋‹ค๋ฉด? erros ๋ Œ๋”๋ง ํ•ด์ฃผ๊ธฐ 
                    ์œ ํšจ์„ฑ type์ด require๋ผ๋ฉด 
                */}

                <label htmlFor="name">์ด๋ฆ„</label>
                <input type="text"
                    id="name"
                    {...register("name",
                        {
                            required: true,
                            maxLength: 10
                        })
                    } />
                {errors.name && errors.name.type === "required" && <p>this name field is required</p>}
                {errors.name && errors.name.type === "maxLength" && <p>your input exceed maximun length</p>}

                <label htmlFor="pw">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
                <input type="password"
                    id="pw"
                    {...register("pw",
                        {
                            required: true,
                            minLength: 8
                        })} />
                {errors.pw && errors.pw.type === "required" && <p>this email field is required</p>}
                {errors.pw && errors.pw.type === "minLength" && <p>your input exceed minLength length</p>}



                <label htmlFor="pwComfirm">๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ</label>
                <input type="password"
                    id="pwComfirm"
                    {...register("pwComfirm")} />



                <label>gender</label>
                <select name="gender" id="gender"
                    {...register("gender", {
                        required: true
                    })}
                >
                    <option value="">select...</option>
                    <option value="male">male</option>
                    <option value="female">female</option>
                </select>

                <ErrorMessage
                    errors={errors}
                    name="gender"
                    render={({ message }) => <p>{message} ์„ฑ๋ณ„์„ ์„ ํƒํ•˜์„ธ์š”</p>}
                />


                <button type="submit">๊ณ„์ •์ƒ์„ฑ</button>
            </form>

        </div>
    )
}
728x90

'Framework > React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

React ํšŒ์›๊ฐ€์ž… API (๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ์ „)  (0) 2022.10.31
React.memo, useCallback(), useMemo()  (0) 2022.10.26
๋ฆฌ์•กํŠธ CRUD ํ•ด๋ณด๊ธฐ  (0) 2022.10.22
React Context API  (0) 2022.10.12
conditional rendering  (0) 2022.10.11

๋Œ“๊ธ€