๋ฐ˜์‘ํ˜•
250x250
Recent Posts
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Hello creators ๐Ÿ™Œ

[_INTRO_] RFH ์„ ์ฒ˜์Œ ํ˜น์€ ๋‹ค์‹œ ๊ณต๋ถ€ํ•œ๋‹ค๋ฉด ๋ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค ๋ณธ๋ฌธ

[ํ”„๋ก ํŠธ์—”๋“œ(FE) ๊ฐœ๋ฐœ] (feat. ์ฃผ๋‹ˆ์–ด)/React Hook Form

[_INTRO_] RFH ์„ ์ฒ˜์Œ ํ˜น์€ ๋‹ค์‹œ ๊ณต๋ถ€ํ•œ๋‹ค๋ฉด ๋ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค

๋ถ€์‹œ๋งค๋‚˜_HA 2024. 12. 1. 23:07
728x90
๋ฐ˜์‘ํ˜•

RHF ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ๊ธฐ๋ณธ ์ง€์‹์œผ๋กœ ์•Œ๋ฉด, ๋„์›€์ด ๋˜๋Š” ๊ฒƒ๋“ค

  1. ๋ฐ˜๋“œ์‹œ 'C:\Users\nextinnovation\Desktop\DJ-DEV\dev_notes\Hard skills\React Hook Form\3_์ œ์–ด ์ปดํฌ๋„ŒํŠธ์™€ ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ(์šฐํ…Œ์ฝ”).md' ๋ฅผ ๊ณต๋ถ€ํ•˜๊ณ  ์˜ฌ ๊ฒƒ
  2. ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฌด์—‡ ์ธ์ง€
  3. react hook form ์ด ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ์˜ ๋‹จ์ ์„ ์–ด๋–ป๊ฒŒ subscribe ๊ธฐ๋Šฅ์„ ํ†ตํ•ด ๊ทน๋ณตํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ๋งฅ๋ฝ์„ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•จ

๋ณต์Šต ์ˆœ์„œ (241120 ์ž‘์„ฑ)

1. input, select, textarea ๋กœ HTML ์„ ์ž‘์„ฑํ–ˆ๋‹ค. ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋Œ€, submit ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ๊ทธ์— ๋”ฐ๋ผ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ input, select, textarea ์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋„๋ก. ( ์ถœ์ฒ˜ : Hard skills\React Hook Form\3_์ œ์–ด ์ปดํฌ๋„ŒํŠธ์™€ ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ(์šฐํ…Œ์ฝ”).md)

- ์—ฌ๊ธฐ์— ์ถ”๊ฐ€๋กœ ์“ฐ๋Š” ๊ฒƒ ๋ณด๋‹ค, ์œ„ ํŒŒ์ผ๋กœ ๊ฐ€์„œ, ๋‹ค์‹œ ๋ณต์Šตํ•˜๋Š”๊ฒŒ ๋‚˜์„ ๋“ฏ

 

2. '์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ’์„ ์ž…๋ ฅํ•˜๊ณ  -> ์ž…๋ ฅํ•œ ๊ฐ’์ด DOM ์œผ๋กœ PUSH ๋˜๊ณ  -> DOM ์— ์žˆ๋Š” ๊ฐ’์„ PULL ํ•ด์˜ค๊ณ  -> RENDER ๋˜๋Š” ๊ณผ์ •'์—์„œ '์‹ ๋ขฐ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ์ถœ์ฒ˜' ์˜ ํ•„์š”์„ฑ์ด ๋‚˜์˜จ๋‹ค. (#โญโญโญ ์‹ ๋ขฐ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ์ถœ์ฒ˜์˜ ๊ฐœ๋…์„ ๋งํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•จ)

(์ถœ์ฒ˜ : Hard skills\React Hook Form\3_์ œ์–ด ์ปดํฌ๋„ŒํŠธ์™€ ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ(์šฐํ…Œ์ฝ”).md)

 

3. ๊ทธ๋Ÿฌ๋ฉด, 'form ๋ฐ์ดํ„ฐ๊ฐ€ ์›€์ง์ด๋Š” ํ๋ฆ„' ์ค‘์—์„œ, '์‹ ๋ขฐ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ์ถœ์ฒ˜' ๋Š” ์–ด๋””์ธ๊ฐ€. (#โญ์ด ํ˜„์ƒ์„ '๋ฐ์ดํ„ฐ๊ฐ€ ์›€์ง์ด๋Š” ๊ด€์ ' ์œผ๋กœ ํ•ด์„ํ–ˆ๋‹ค๋Š” ๊ฒƒโญโญ)

๊ทธ๋Ÿฌ๋ฉด, '์‹ ๋ขฐ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ์ถœ์ฒ˜' ๋ผ๋Š” ๊ฒƒ์ด 'ํ•˜๋‚˜์˜ ์ƒํƒœ๋Š” ํ•œ ๊ณณ์— ์žˆ์–ด์•ผ ํ•œ๋‹ค.' ๋ผ๋ฉด, 'ํ•˜๋‚˜์˜ ์ƒํƒœ' ๋Š” ๋ญ์ง€? ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ƒํƒœ๋Š” '์–ด๋””์— ์žˆ์ง€?' 

๊ทธ๋Ÿผ ์šฐ์„ , '์ƒํƒœ' ๋ผ๋Š” ๊ฑด ๋ญ์ง€? 

 

4. '์ƒํƒœ๋ž€?' '์ƒํƒœ์˜ ๊ฐœ๋…์— ๋น„์ถ”์–ด๋ดค์„ ๋•Œ, ์‹ ๋ขฐ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ์ถœ์ฒ˜' ๋ž€? '๊ทธ๋Ÿฌ๋ฉด, form ํƒœ๊ทธ์—๋Š” ์–ด๋–ค ์ƒํƒœ ๋“ค์ด ์žˆ๊ณ , ์–ด๋””์—์„œ ๊ด€๋ฆฌ๋˜๋Š”๊ฐ€?' (#์‹ ํ˜ธ๋“ฑ ์˜ˆ์‹œ)

- ์ผ๋ฐ˜์ ์ธ ์‹ ํ˜ธ๋“ฑ์€ ๋นจ๊ฐ•, ์ฃผํ™ฉ, ์ดˆ๋ก์˜ ์ƒ‰์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ๊ฐ ์ •์ง€, ์œ„ํ—˜, ํ†ต๊ณผ์˜ ์˜๋ฏธ๊ฐ€ ์žˆ์ง€. 

- ์‹ ํ˜ธ๋“ฑ์€ 3๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ฐ–๋Š”๋‹ค. 

- ์ด๋•Œ, ์‹ ํ˜ธ๋“ฑ์ด '์‹ค์ œ๋กœ ์ดˆ๋ก๋ถˆ' ์„ ๋น„์ถ”๋ฉด, ์‹ ํ˜ธ๋“ฑ์€ 'ํŒŒ๋ž€๋ถˆ์„ ๋น„์ถ”๋Š” ์ƒํƒœ' ๊ฐ€ ๋œ๋‹ค. 

- ์‹ ํ˜ธ๋“ฑ์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 3๊ฐœ ์ด๊ณ , ๊ทธ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์„ ํƒ๋˜์–ด, ์‹ค์ œ๋กœ ํ‘œํ˜„๋œ ๊ฒƒ์ด '์ƒํƒœ' ๋‹ค. 
(์ถœ์ฒ˜ : https://developer.mozilla.org/en-US/docs/Glossary/State_machine)
- ๊ทธ๋Ÿฌ๋ฉด, ์ด ์‹œ์Šคํ…œ์ด ํ‘œํ˜„ํ•˜๊ณ  ์žˆ๋Š” '๋ชจ๋“  ์ƒํƒœ' ๊ฐ€ ์กด์žฌํ•˜๊ณ , 
- ๊ทธ ์ค‘ ํ•˜๋‚˜์˜ ์ƒํƒœ๊ฐ€ ๋˜๋ฉด, ๊ทธ ์ƒํƒœ์— ๋Œ€ํ•œ ๊ฐ’์€, 'ํ•œ ๊ณณ' ๋งŒ ๊ธฐ์–ตํ•œ๋‹ค! ๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. (#โญโญโญโญโญโญ)
- form ํƒœ๊ทธ์˜ ์ž์‹์œผ๋กœ input, select, textarea ๋“ฑ์ด ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. 

- ์ด์ œ, input ํƒœ๊ทธ ์—๋งŒ ์ง‘์ค‘ํ•œ๋‹ค๋ฉด, input ํƒœ๊ทธ์˜ ์ƒํƒœ๋Š”, value ์•ˆ์— ๋‹ด๊ธด ๊ฐ’์ด ๋˜๋Š”๊ฒŒ ๋งž์ง€ ์•Š์„๊นŒ? ์™œ๋ƒ๋ฉด, input ํƒœ๊ทธ๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ค‘ ํ•˜๋‚˜์˜ ๊ฒฝ์šฐ๊ฐ€ ๋“ค์–ด์˜จ ๊ฑฐ๋‹ˆ๊นŒ. 

- ๊ทธ๋ฆฌ๊ณ  ์ด '์ƒํƒœ' (value) ๋Š”, value attribute ๋กœ ๋“ค์–ด๊ฐ€๊ณ  -> value attribute ๋Š” DOM ์•ˆ์— ์žˆ์œผ๋‹ˆ๊นŒ, DOM ์— push ๊ฐ€ ๋œ๋‹ค. 

- ๊ทธ๋Ÿฌ๋ฉด ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋„, DOM ์—์„œ PULL ์„ ํ†ตํ•ด ์ตœ์‹ ํ™” ํ•˜๊ฒŒ ๋œ๋‹ค. 


CF. FORM ๊ณผ ๊ด€๋ จ๋œ ์ผ๋ จ์˜ ๊ณผ์ •์„ STATE MACHINE ๊ด€์ ์—์„œ ๋ณธ๋‹ค๋ฉด
- ์ด๊ฒƒ๋“ค์ด ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” '์ƒํƒœ' ๋กœ๋Š” 
    1) ๋Œ€๊ธฐ์ค‘ (์•„๋ฌด๊ฒƒ๋„ ์ž‘์„ฑ ์•ˆ ๋˜๋Š” ์ƒํƒœ) 
    2) ์ž‘์„ฑ์ค‘์ธ ์ƒํƒœ 
    3) ์ž‘์„ฑ ์™„๋ฃŒ๋ฅผ ํด๋ฆญํ•œ ์ƒํƒœ 
    4) ์ž‘์„ฑ ๊ฒฐ๊ณผ๋ฌผ์ด ์œ ํšจํ•œ์ง€ ํ™•์ธํ•˜๋Š” ์ „ํ™˜ ๊ณผ์ • (transition) 
    5) ์ œ์ถœ ์„ฑ๊ณต ๋˜๋Š” ์‹คํŒจ์˜ ์ƒํƒœ
        - ์‹คํŒจ์˜ ๊ฒฝ์šฐ, ๋‹ค์‹œ ์ž‘์„ฑํ•˜๋Š” ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ฒŒ ๋จ

- ๊ทธ๋Ÿฌ๋ฉด, ์œ„์— ์ž‘์„ฑํ•œ ๋‹ค์–‘ํ•œ ์ƒํƒœ๋Š” ๋ญ˜๊นŒ? form ์ œ์ถœ ๊ณผ์ •์—์„œ ์‚ฌ์šฉ์ž ํ๋ฆ„? ์‚ฌ์šฉ์ž์˜ ์ƒํƒœ? ๋ผ๊ณ  ๋ด์•ผ ํ• ๊นŒ? ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๊ณผ์ •์„ ์ปจํŠธ๋กค ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์•ผ ํ• ๊นŒ? 
stateDiagram-v2
    state "๋Œ€๊ธฐ (Waiting)" as Waiting
    state "์ž‘์„ฑ ์ค‘ (In Progress)" as InProgress
    state "์ œ์ถœ (Submission)" as Submission
    state "์ œ์ถœ ์‹คํŒจ (Submission Failure)" as SubmissionFailure
    state "์ œ์ถœ ์„ฑ๊ณต (Submission Success)" as SubmissionSuccess
    state "๋‹ค์Œ ํŽ˜์ด์ง€ (Next Page)" as NextPage

    Waiting --> InProgress : Start writing
    InProgress --> Submission : Click Submit
    Submission --> SubmissionFailure : Validation Failed
    Submission --> SubmissionSuccess : Validation Passed
    SubmissionFailure --> InProgress : Retry
    SubmissionSuccess --> NextPage : Proceed

5. ๊ทธ๋Ÿฌ๋ฉด React Hook Form ์˜ '์‹ ๋ขฐ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ์ถœ์ฒ˜' ๋ž€?

- '์‹ ๋ขฐ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ์ถœ์ฒ˜' ๋ž€ 'ํ•˜๋‚˜์˜ ์ƒํƒœ๋Š” ํ•œ ๊ณณ' ์—๋งŒ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ ์ด๋‹ค. 

- React ๋Š” value ์™€ ๊ฒฐํ•ฉ๋œ useState ์˜ state ๋ฅผ 'Single Source of Truth' ๋กœ ์‚ฌ์šฉํ•˜์ง€๋งŒ 
    - state ๋กœ ๋ณด๋Š” ์ด์œ ๋Š”, form ์ œ์ถœ์„ STATE MACHINE ๊ด€์ ์—์„œ ๋ณผ ๋•Œ, '์ž‘์„ฑ' ๋‹จ๊ณ„ ๋‹ค์Œ '์ œ์ถœ' ์ธ๋ฐ, ์ด๋•Œ, validation ๊ฒ€์‚ฌ๋ฅผ ํ•  ๋•Œ, state ์—์„œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜๋„ ์žˆ์ง€ ์•Š์„๊นŒ. (#โญโญโญโญโญ)
    (๋ฌผ๋ก , ๊ทธ๋ƒฅ, ๋‹จ์ˆœํ•˜๊ฒŒ, react ์˜ useState ์˜ state ๋ผ๊ณ ๋„ ํ•˜๊ธด ํ•˜๋Š”๋ฐ...)

- React Hook Form ์€ DOM ์— ๊ฐ’์„ push ํ•˜๊ณ , ํ•ด๋‹น ํƒœ๊ทธ์˜ ๊ฐ’์ด ํ•„์š”ํ•˜๋ฉด, DOM ์—์„œ ๊ฐ’์„ PULL ํ•ด์˜จ๋‹ค. 
    - DOM ์— PUSH ํ•˜๋Š” ์›๋ฆฌ๋Š”, input ํƒœ๊ทธ ๋ณธ์—ฐ์˜ push ๋กœ์ง์„ ๋”ฐ๋ฅธ๋‹ค. 
    - DOM ์—์„œ PULL ํ•˜๋Š” ์›๋ฆฌ๋Š” react ์˜ useRef ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DOM ์— ์žˆ๋Š” ๊ฐ’์„ PULL ํ•ด์˜จ๋‹ค. 
    - input ์— ๋„ฃ์€ ๊ฐ’์ด ํ•„์š”ํ•œ ์ˆœ๊ฐ„์€, submission or validation. ์ด๋•Œ, DOM ์—์„œ ๊ฐ’์„ PULL ํ•ด์˜จ๋‹ค. (#โญโญโญโญโญ form ๊ณผ์ •์„ STATE MACHINE ๊ด€์ ์œผ๋กœ ๋ณด๊ณ , ์–ด๋””์—์„œ PULL ํ•ด์˜ค๋Š”๊ฐ€๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด๋ฉด ๋œ๋‹ค!!!)

- ๊ทธ๋Ÿฌ๋ฉด, ๊ฐ ์ƒํƒœ ๋ฐ ์ „์ด(transition) ๊ณผ์ •์„ react hook form ์ด ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌํ•˜๋Š”์ง€๊ฐ€ ๋‚˜์˜จ๋‹ค.

6. FORM ์‚ฌ์šฉ์ž ํ๋ฆ„์„ STATE MACHINE ๊ด€์ ์—์„œ ๋ฐ”๋ผ๋ณด๊ณ , ๊ฐ STATE ๋ฐ Transition ์„ react hook form ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ

stateDiagram-v2
    state "๋Œ€๊ธฐ (Waiting)" as Waiting
    state "์ž‘์„ฑ ์ค‘ (In Progress)" as InProgress
    state "์ œ์ถœ (Submission)" as Submission
    state "์ œ์ถœ ์‹คํŒจ (Submission Failure)" as SubmissionFailure
    state "์ œ์ถœ ์„ฑ๊ณต (Submission Success)" as SubmissionSuccess
    state "๋‹ค์Œ ํŽ˜์ด์ง€ (Next Page)" as NextPage

    Waiting --> InProgress : Start writing
    InProgress --> Submission : Click Submit
    Submission --> SubmissionFailure : Validation Failed
    Submission --> SubmissionSuccess : Validation Passed
    SubmissionFailure --> InProgress : Retry
    SubmissionSuccess --> NextPage : Proceed

WAITING ์ƒํƒœ

- ํ•ด๋‹น ์ƒํƒœ์—์„œ ๋ฒŒ์–ด์ง€๋Š” ์ผ :
Inputs are idle, and no user interaction has occurred.
- RHF Role:
Initialize the form with defaultValues.
const { register, handleSubmit } = useForm({
  defaultValues: { username: '', email: '' },
});

InProgress ์ƒํƒœ

- What Happens:
The user starts typing or interacting with the form.

- RHF Role:
Track input values in the DOM (uncontrolled components).
<input {...register('username')} />

Submission ์ œ์ถœ

- What Happens:
The user clicks the submit button, and RHF pulls values from the DOM.
- RHF Role:
handleSubmit is triggered, and RHF retrieves values via useRef.
const onSubmit = (data) => {
  console.log(data); // Data is pulled from the DOM
};
<form onSubmit={handleSubmit(onSubmit)}>
  <input {...register('username')} />
  <button type="submit">Submit</button>
</form>

Submission Failure:

- What Happens:
Validation fails due to incorrect or missing values.
- RHF Role:
Perform validation using rules passed to register.
Error states are managed in formState.errors.
<input
  {...register('email', { required: 'Email is required' })}
/>
{formState.errors.email && <p>{formState.errors.email.message}</p>}

Submission Success

- What Happens:
Validation passes, and the form is submitted successfully.
- RHF Role:
Proceed with the form submission logic in the onSubmit callback.
const onSubmit = async (data) => {
  // Handle API submission here
  console.log('Submission Successful', data);
};

Next Page

- What Happens:
After successful submission, the user navigates to the next page.
- RHF Role:
Redirect or reset the form using RHF utilities like reset().
const { reset } = useForm();
const onSubmit = (data) => {
  console.log(data);
  reset(); // Reset the form after successful submission
};

์ „์ฒด flow


import React from 'react';
import { useForm } from 'react-hook-form';

function FormExample() {
  const {
    register,
    handleSubmit,
    formState: { errors },
    reset,
  } = useForm({
    defaultValues: { username: '', email: '' },
  });

  const onSubmit = async (data) => {
    try {
      console.log('Validation Passed:', data);
      // Simulate successful submission
      reset();
    } catch (err) {
      console.error('Submission Failed:', err);
    }
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <div>
        <label>Username</label>
        <input {...register('username', { required: 'Username is required' })} />
        {errors.username && <p>{errors.username.message}</p>}
      </div>
      <div>
        <label>Email</label>
        <input {...register('email', { required: 'Email is required' })} />
        {errors.email && <p>{errors.email.message}</p>}
      </div>
      <button type="submit">Submit</button>
    </form>
  );
}

export default FormExample;

7. ์ด๋Ÿฌํ•œ ํ๋ฆ„์—์„œ REACT HOOK FORM ๊ฐ•์˜๋ฅผ ์–ด๋–ป๊ฒŒ ๋“ค์„ ๊ฒƒ ์ธ๊ฐ€

- ์œ„์˜ ์ด๋ก ์ ์ธ ๋ถ€๋ถ„์ด ์ฝ”๋“œ์ƒ ๊ตฌ์ฒด์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„์ด ๋˜๋Š”๊ฐ€! ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ธฐ
    - ์ฝ”๋“œ๋ฅผ ์ฝ์œผ๋ฉด์„œ ํ•ด๋‹น ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ฃผ์„ ๋‹ฌ๊ธฐ

- ์„ธ์„ธํ•œ ๋ฌธ๋ฒ•์ ์ธ ๊ตฌํ˜„์ฒด๋ฅผ ์ดํ•ดํ•ด์•ผ ํ•จ 
    ex) {...register('customer')} ์—์„œ ... ์ด๋ ‡๊ฒŒ ํŽผ์ณ์ฃผ๋Š” ์ด์œ ๊ฐ€ ๋ฌด์—‡์ธ์ง€ 

8. ์ง€๊ธˆ๊นŒ์ง€ ์ดํ•ดํ•œ ๋ถ€๋ถ„ ์ฃผ์„ ๋‹ฌ๊ธฐ

import React from 'react';
import { useForm } from 'react-hook-form';

function FormExample() {
  const {
    register, 
        /* [register ๋ฉ”์„œ๋“œ]
            1) parameter 
            2) return
                - name, onBlur, onChange, ref ๋ฅผ return ํ•œ๋‹ค. 
                - ์ด๊ฒŒ spread operator ๋กœ ํ’€๋ฆฌ๋ฉด์„œ input ์˜ ์†์„ฑ์— ๋ถ™๊ฒŒ ๋œ๋‹ค. 
        */
    handleSubmit, 
        /* handleSubmit
            - ์ œ์ถœ ํ–ˆ์„ ๋•Œ์˜ ์ƒํƒœ๋ฅผ ์ฒ˜๋ฆฌ ํ•œ๋‹ค.
        */
    formState: { errors }, // ์—๋Ÿฌ ์ƒํƒœ ์ฒ˜๋ฆฌ 
    reset, // ์ด๊ฑด? 
  } = useForm({
    defaultValues: { username: '', email: '' },
  });

  const onSubmit = async (data) => {
    try {
      console.log('Validation Passed:', data);
      // Simulate successful submission
      reset();
    } catch (err) {
      console.error('Submission Failed:', err);
    }
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <div>
        <label>Username</label>
        <input {...register('username', { required: 'Username is required' })} />
        {errors.username && <p>{errors.username.message}</p>}
      </div>
      <div>
        <label>Email</label>
        <input {...register('email', { required: 'Email is required' })} />
        {errors.email && <p>{errors.email.message}</p>}
      </div>
      <button type="submit">Submit</button>
    </form>
  );
}

export default FormExample;

9. ์ถ”๊ฐ€๋กœ ์•Œ๊ณ  ์‹ถ์€ ๊ฒƒ

1. STATE MACHINE ์„ ๊ทธ๋ฆฌ๊ณ  - ๊ฐ ๊ด€๋ฆฌ ํฌ์ธํŠธ๋ฅผ ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ณด๊ธฐ 

2. ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋””์—์„œ ๊ฐ€์ ธ์™€์•ผ ํ•˜๋Š”์ง€๋ฅผ, ๋‹ค์ด์–ด๊ทธ๋žจ์œผ๋กœ ์–ด์ผ€ ๊ทธ๋ ค์•ผ ํ•˜๋Š”์ง€ ์ƒ๊ฐํ•ด๋ณด๊ธฐ

3. react hook form ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉด์„œ, ๊ทธ๊ฒƒ ๋Œ€๋กœ์˜ ํ๋ฆ„์„ ์ ์–ด๋ณด๋ฉด ์ข‹์„ ๊ฑฐ ๊ฐ™์€๋ฐ. 
    - ์ด๊ฒŒ ๋‹ค๋ฅธ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ณผ ๋•Œ๋„ ๋น„์Šทํ•˜๊ฒŒ ์ด ๋ฐฉ์‹์ด ์ ์šฉ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ 
728x90
๋ฐ˜์‘ํ˜•
Comments