๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ Cross Platform/React Native

[React Native] React Hook Form ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ (Using React Hook Form)

by Fomagran ๐Ÿ’ป 2022. 6. 22.
728x90
๋ฐ˜์‘ํ˜•

 

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์ €๋ฒˆ ์‹œ๊ฐ„์— Form์„ ๋งŒ๋“ค์–ด ํ•ด๋‹น Screen์— ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด ๋ณด์•˜๋Š”๋ฐ์š”.

 

(ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ง„ํ–‰๋˜๋‹ˆ ์•„์ง ์•ˆ๋ณด์‹  ๋ถ„๋“ค์€ ์—ฌ๊ธฐ ์—์„œ ๋ณด๊ณ ์™€ ์ฃผ์„ธ์š”!)

 

๊ฐ„๋‹จํ•˜๊ณ  ๋” ํšจ์œจ์ ์œผ๋กœ Form์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์–ด ์‚ฌ์šฉํ•ด ๋ณด๊ณ  ๊ธ€์„ ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~!

 

์–ธ์–ด๋Š” TypeScript, ํ™˜๊ฒฝ์€ Expo๋กœ ์ง„ํ–‰ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!


React Hook Form์ด๋ž€?

 

React Hook Form์€ Form์„ ์‚ฌ์šฉ ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

 

๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์—์„  ํ•œ ์ค„๋กœ "์œ ์šฉํ•˜๊ณ  ํ™•์žฅ ๊ฐ€๋Šฅํ•˜๋ฉฐ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฌ์šด ๊ณ ์„ฑ๋Šฅ ํผ ๊ฒ€์ฆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ" ๋ผ๊ณ  ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.

 

์žฅ์ ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 

๋” ์ž์„ธํ•œ ๋‚ด์šฉ์„ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€๋กœ ๊ฐ€์„œ ํ™•์ธํ•ด ๋ณด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค!

 

 

Get Started

Performant, flexible and extensible forms with easy-to-use validation.

react-hook-form.com


Install

 

React Hook Form์„ ์„ค์น˜ํ•ด ์ค๋‹ˆ๋‹ค.

 

npm install react-hook-form

 


AddPersonForm

 

๋จผ์ € useForm๊ณผ Controller๋ฅผ import ํ•ด์ค๋‹ˆ๋‹ค.

 

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

 

๋จผ์ € control๊ณผ handleSubmit ์œผ๋กœ useForm์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

control์€ form ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜์ด๊ณ  handleSubmit์€ form์„ ์ œ์ถœํ•  ๋•Œ onSubmit ํ”„๋กœํผํ‹ฐ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

export function AddPersonForm({ onSubmit }: PersonFormProps) {
  const { control, handleSubmit } = useForm();
      ...

 

์•„๋ž˜์™€ ๊ฐ™์ด Controller ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ™œ์šฉํ•˜๋Š”๋ฐ์š”.

 

control: ์œ„์—์„œ ๋งŒ๋“  control์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

rules:  ํผ์ด ๋ฐ˜๋“œ์‹œ ์ž…๋ ฅ๋˜์–ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์ ์–ด์ค๋‹ˆ๋‹ค.

name:  ํผ์˜ ์ด๋ฆ„์„ ์ •ํ•ด์ค๋‹ˆ๋‹ค.

defaultValue: ํผ์ด ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์„ ๋•Œ ๊ธฐ๋ณธ๊ฐ’์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

render: onChange์™€ value ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„ TextInput์˜ onChangeText์™€ value์— ๋ฐ”์ธ๋”ฉ ํ•ด์ฃผ์–ด ๊ฐ’์ด ๋ฐ”๋€Œ๋ฉด ์ฆ‰์‹œ TextInput์˜ ์ž…๋ ฅ๊ฐ’์ด ๋ฐ”๋€Œ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

(์ด ์™ธ์—๋„ errors,maxLength ๋“ฑ ๋‹ค์–‘ํ•œ validation์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)

 

  return (
    <View>
      <Controller
        control={control}
        rules={{ required: true }}
        name="name"
        defaultValue={""}
        render={({ field: { onChange, value } }) => (
          <TextInput
            onChangeText={onChange}
            value={value}
            style={styles.input}
            placeholder="Enter your name"
          />
        )}
      />

 

Button ์ปดํฌ๋„ŒํŠธ์— onPress๊ฐ€ ์‹คํ–‰๋  ๋•Œ ์œ„์—์„œ ๋งŒ๋“  handleSubmit์„ onSubmit ํ”„๋กœํผํ‹ฐ๋กœ data๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

 

      <Button
        onPress={handleSubmit((data) => onSubmit(data as PersonForm))}
        title="Submit"
      ></Button>

์‹คํ–‰ ํ™”๋ฉด

 

ํผ์— ๊ฐ’์„ ๋„ฃ๊ณ  Submit ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น ํผ์— ์ž…๋ ฅ๋œ ๊ฐ’๋“ค์ด alert๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€