[React] Input ํƒœ๊ทธ clear ๋ฒ„ํŠผ ์žฌ๋™์ž‘ ์•ˆ ํ•˜๋Š” ๋ฒ„๊ทธ
728x90

Today, What I learned?

์ฃผ๋ง์ฏค์— ๋””์ž์ธ ์ˆ˜์ •์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ–ˆ๋˜ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์ธ ์ธํ’‹ ํƒœ๊ทธ ํด๋ฆฌ์–ด ๋ฒ„ํŠผ์ด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ์ด์Šˆ..

๋ฒ„๊ทธ๋ฅผ ํ•ด๊ฒฐํ•˜๋‹ค๊ฐ€ ์ œ์–ด ์ปดํฌ๋„ŒํŠธ์™€ ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ์— ๋Œ€ํ•ด์„œ๋„ ์ฐพ๊ฒŒ ๋˜์–ด์„œ ํ•œ ๋ฒˆ ์ •๋ฆฌํ•ด ๋ณธ๋‹ค.

 

 

 

์›์ธ ํŒŒ์•…

์ผ๋‹จ ์ฒ˜์Œ์€ ๋™์ž‘์ด ์ž˜ ๋˜๋Š”๋ฐ, ํ•œ ๋ฒˆ ์ง€์šฐ๊ณ  ๋‚œ ๋’ค๋ถ€ํ„ฐ ์ œ๋Œ€๋กœ ๋™์ž‘์ด ๋˜์ง€ ์•Š์•˜๋‹ค.

๊ทธ๋ž˜์„œ ๋กœ๊ทธ๋ฅผ ์ฐ์–ด๋ณด๋‹ˆ.. state๋Š” ๋ณ€๊ฒฝ์ด ์ž˜ ๋˜์—ˆ์ง€๋งŒ input์ฐฝ์— ๋น„์–ด๋ฒ„๋ฆฐ ๊ฐ’์ด ๋‹ค์‹œ ๋ฐ˜์˜์ด ์•ˆ ๋˜๊ณ  ์žˆ๋Š” ์ƒํƒœ..

input ํƒœ๊ทธ์— state์˜ ๊ฐ’์ด ์ฆ‰๊ฐ์ ์œผ๋กœ ๋ฐ˜์˜์ด ๋˜์ง€ ์•Š๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค.

 

 

์›์ธ์€ ๋ฐ”๋กœ defaultValue !..

๋งˆ์ดํŽ˜์ด์ง€๋Š” ๋Œ€์ฒด๋กœ ์ˆ˜์ •ํ•˜๋Š” ์˜์—ญ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด์˜์‹์ ์œผ๋กœ value ๋Œ€์‹  defaultValue๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. (๊ธฐ๋ณธ๊ฐ’์ด ์กด์žฌํ•˜๋‹ˆ๊นŒ)

๊ด€๋ จํ•ด์„œ ์„œ์น˜ ํ•˜๋‹ค๊ฐ€ ์ œ์–ด ์ปดํฌ๋„ŒํŠธ(Controlled component)์™€ ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ(Uncontrolled component)์— ๋Œ€ํ•œ ๊ธ€์„ ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค. ์ดˆ๋ฉด์ธ ์šฉ์–ด๋‹ค... ๐Ÿ‘€

 

 

์ œ์–ด ์ปดํฌ๋„ŒํŠธ vs ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ

์ œ์–ด ์ปดํฌ๋„ŒํŠธ

๋ฆฌ์•กํŠธ ๊ณต์‹ ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด ์ด๋ ‡๋‹ค.

 

<input>, <textarea> ๋“ฑ์˜ ํผ ์—˜๋ฆฌ๋จผํŠธ๋Š” ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํƒœ๊ทธ ์ž์‹ ์˜ state๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋Š” ํผ์— ๋ฐœ์ƒํ•˜๋Š” ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ์‹์˜ ํผ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ œ์–ด ์ปดํฌ๋„ŒํŠธ๋ผ๊ณ  ํ•œ๋‹ค.

์œ„์˜ ๋ง์„ ์ข€ ํ’€์–ด์“ฐ๋ฉด, ๋ณดํ†ต input ํƒœ๊ทธ์—๋Š” onChange ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๋‹ฌ๋ ค์žˆ๋‹ค.

 

  • ์‚ฌ์šฉ์ž๊ฐ€ ํƒœ๊ทธ์— ๊ฐ’์„ ์ž…๋ ฅํ•œ๋‹ค
  • ์ž…๋ ฅํ•  ๋•Œ๋งˆ๋‹ค onChange ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋™์ž‘ > setState๋ฅผ ํ†ตํ•ด state ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
  • ๋ณ€๊ฒฝ๋œ state ๊ฐ’์„ ํƒœ๊ทธ์˜ value ๊ฐ’์— ํ• ๋‹น
 const [email, setEmail] = useState<string>('');

 const changeEmail = (event: ChangeEvent<HTMLInputElement\>) => {
    setEmail(event.target.value);
  };

    <\CustomInput
            id="email"
            labelText="์ด๋ฉ”์ผ"
            placeholder="์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
            value={email} // <<<<
            onChange={changeEmail} // <<<<
    />

์ด๋ ‡๊ฒŒ ํผ ํƒœ๊ทธ๊ฐ€ ์ปดํฌ๋„ŒํŠธ์˜ ์ œ์–ด๋ฅผ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์ œ์–ด ์ปดํฌ๋„ŒํŠธ.

 

 

 

๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ

๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ๋Š” ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์•„๋‹Œ, DOM ์ž์ฒด์—์„œ ํผ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ค๋ฃจ์–ด์ง„๋‹ค.
state์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋Œ€์‹ ์— useRefํ›…์„ ์ด์šฉํ•ด์„œ DOM์—์„œ ํผ ๊ฐ’์„ ์ง์ ‘ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ์‹!

const emailRef = useRef<HTMLInputElement | null\>(null);

const handleSubmit = () => {
   const name = emailRef.current.value;
};

    return(
        <form onSubmit={handleSubmit}>
            <input 
                 type="text"
                ref={inputRef}
            />
            <button>์ž…๋ ฅ</button>
        </form>
    );  
  • ์‚ฌ์šฉ์ž๊ฐ€ ํƒœ๊ทธ์— ๊ฐ’์„ ์ž…๋ ฅ
  • ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ref๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ์–ป์Œ

 

๋™์ž‘๊ณผ์ •์„ ๋ณด๋ฉด ์ปดํฌ๋„ŒํŠธ์˜ ์ œ์–ด๋ฅผ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.

 

 

๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ์— ๋Œ€ํ•ด์„œ ๊ณต์‹๋ฌธ์„œ์˜ ๋‚ด์šฉ์„ ๊ทธ๋Œ€๋กœ ๋“ค๊ณ  ์™”๋‹ค.

React ๋ Œ๋”๋ง ์ƒ๋ช…์ฃผ๊ธฐ์—์„œ ํผ ์—˜๋ฆฌ๋จผํŠธ์˜ value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋Š” DOM์˜ value๋ฅผ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด React ์ดˆ๊นƒ๊ฐ’์„ ์ง€์ •ํ•˜์ง€๋งŒ, ๊ทธ ์ดํ›„์˜ ์—…๋ฐ์ดํŠธ๋Š” ์ œ์–ดํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์— value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๋Œ€์‹  defaultValue๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ ๋œ ํ›„์— defaultValue ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ๋ณ€๊ฒฝํ•ด๋„ DOM์˜ ๊ฐ’์ด ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋‚ด์šฉ์ด ๋ฐ”๋กœ ๊ธฐ์กด ์ฝ”๋“œ์—์„œ value๊ฐ’์ด ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š์•˜๋˜ ์ด์œ !..

 

๋‚˜๋Š” ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ฐ’์ด ํ•„์š”ํ•˜๋‹ˆ ๋งค๋ฒˆ ์žฌ๋ Œ๋”๋ง์ด ๋˜๋Š” ์ œ์–ด ์ปดํฌ๋„ŒํŠธ ๋ฐฉ์‹์œผ๋กœ ํผ์ด ๊ด€๋ฆฌ๋˜์–ด์•ผ ํ–ˆ๋‹ค..
๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ์— ํ•ด๋‹นํ•˜๋Š” defaultValue ๋ผ๋Š” ์†์„ฑ๊ฐ’์ด ์ ํ•ฉํ•˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ์ด๋‹ค!

๊ทธ๋ž˜์„œ value๋กœ ๋ณ€๊ฒฝํ•ด ์ฃผ์—ˆ๋”๋‹ˆ ์•„์ฃผ ์ž˜ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 

 

 

์˜๋„์น˜ ์•Š๊ฒŒ ์ƒ๊ฐ๋ณด๋‹ค ๋งŽ์€ ๊ฒƒ์„ ์•Œ๊ฒŒ ๋œ ๋ฒ„๊ทธ์˜€๋‹ค..! ๐Ÿ‘

 

 

์ฐธ๊ณ 

https://ko.reactjs.org/docs/uncontrolled-components.html

 

๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

 

 

[React] input์š”์†Œ์— value์™€ defaultValue์˜ ์ฐจ์ด์ 

https://reactjs-kr.firebaseapp.com/docs/uncontrolled-components.htmlhttps://blog.logrocket.com/controlled-vs-uncontrolled-components-in-reac

velog.io

 

 

728x90