๐Ÿšจ setStateAction ํƒ€์ž…์—๋Ÿฌ

๐Ÿšจ ์—๋Ÿฌ ๋ฐœ์ƒ

todolist๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ๊ถ๊ธˆํ•œ ์ ์ด ์ƒ๊ฒผ๋‹ค.

const [createInput, setCreateInput] = useState("");

const onChangeCreateInput = (e: ChangeEvent<HTMLInputElement>) => {
  const value = e.target;
  setCreateInput(value);
  // Error: Argument of type 'EventTarget & HTMLInputElement' is not assignable to parameter of type 'SetStateAction<string>'.ts(2345)
};

value์˜ ์ค‘๊ด„ํ˜ธ์˜ ์˜๋ฏธ๊ฐ€ ๊ถ๊ธˆํ•ด์กŒ๋‹ค.

์ค‘๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋กœ ์„ ์–ธํ–ˆ์„๋•Œ๋Š” ์—๋Ÿฌ๊ฐ€ ์•ˆ๋‚˜๋Š”๋ฐ ์ค‘๊ด„ํ˜ธ๋ฅผ ์—†์• ๋‹ˆ๊นŒ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

ํ•ด๊ฒฐ์„ ์œ„ํ•ด okky์— ์งˆ๋ฌธํ•ด๋ณด์•˜๋‹ค.

โ” ์—๋Ÿฌ ์›์ธ

EventTarget & HTMLInputElement ํƒ€์ž…์˜ ์ „๋‹ฌ ์ธ์ž(ํ˜ธ์ถœํ•  ๋•Œ)๋Š” SetStateAction์˜ ํƒ€์ž…์˜ ํŒŒ๋ผ๋ฏธํ„ฐ(์ •์˜ํ•  ๋•Œ)์— ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค.

์ฆ‰, ์ด๋ฒคํŠธ ๊ฐ์ฒด์ธ e์˜ ํƒ€์ž…๊ณผ setCreateInput์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํƒ€์ž…์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๋ถ€๋ถ„์˜ ์ฝ”๋“œ์—์„œ

e.target์˜ ํƒ€์ž…์„ ๋ฐ›์€ value์˜ ํƒ€์ž…์€ EventTarget & HTMLInputElement์ธ๋ฐ,

์‚ฌ์šฉํ•˜๋Š” ์ƒํƒœ ๋ณ€๊ฒฝ ํ•จ์ˆ˜ setCreateInput์˜ ํƒ€์ž…์ด SetStateAction<string> ์ฆ‰, string์ด๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž…์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋‹ค.

๐Ÿ”จ ์—๋Ÿฌ ํ•ด๊ฒฐ

const{ value } = e.target์€ const value = e.target.value์™€ ๊ฐ™์€ ์˜๋ฏธ์ด๋‹ค.

ํƒ€์ž…์„ ๋งž์ถฐ ์“ฐ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฒคํŠธ ๊ฐ์ฒด ์•ˆ์˜ ๊ฐ’์„ ๊บผ๋‚ด์„œ ์จ์•ผ ํ•œ๋‹ค.

์ถœ์ฒ˜

OKKY

Leave a comment