Redux-toolkit ํˆฌ๋‘๋ฆฌ์ŠคํŠธ๋ฅผ TS๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•˜๋ฉฐ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๋ชจ์Œ (1)
728x90

Today, What I learned?

์ผ์ฐจ์ ์œผ๋กœ ํˆฌ๋‘๋ฆฌ์ŠคํŠธ๋ฅผ redux-toolkit์œผ๋กœ ๋ฆฌํŒฉํ† ๋ง ํ•œ ํ›„์— ๋ณธ๊ฒฉ์ ์œผ๋กœ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์— ๋“ค์–ด๊ฐ€๋ฉฐ ๋งŒ๋‚˜๊ฒŒ ๋œ ์˜ค๋ฅ˜๋“ค์„ ๊ธฐ๋กํ•ด๋ณธ๋‹ค.
์—๋Ÿฌ๊ฐ€ ๊ผฌ๋ฆฌ์— ๊ผฌ๋ฆฌ๋ฅผ ๋ฌผ๊ณ  ๋ฐœ์ƒํ•ด์„œ ๋ชจ๋“  ์—๋Ÿฌ๊ฐ€ ์™„์ „ ํ•ด๊ฒฐ๋œ ๊ฒƒ์€ ์•„๋‹ˆ๊ณ  ํ˜„์žฌ ์ง„ํ–‰ํ˜•์ด๋‹ค.. ๐Ÿ˜‡
๋‚ด์ผ์ฏค์—” ๋‹ค ์™„์„ฑํ•˜๊ธฐ ์•Š์„๊นŒ!..

 

 

๐Ÿšง useRef()์˜ focus ๊ด€๋ จ ์—๋Ÿฌ

Property 'focus' does not exist on type 'string'

Property 'focus' does not exist on type 'string'

 

useRef()์˜ ํƒ€์ž…์ด ์ œ๋Œ€๋กœ ์ •ํ•ด์ ธ์žˆ์ง€ ์•Š์•„ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ..

์ฐพ์•„๋ณด๋‹ˆ useRef๋ฅผ DOM ์กฐ์ž‘ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด HTMLํƒœ๊ทธ๋ฅผ ์ง€์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค๊ณ ..

์ง€์—ญ๋ณ€์ˆ˜๋กœ useRef๋ฅผ ์“ธ ๊ฒฝ์šฐ์—๋Š” string์ด๋‚˜, number ๊ฐ™์€ ์“ฐ์ž„์— ํ•„์š”ํ•œ ํƒ€์ž…์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

  const newTitleInput = useRef<HTMLInputElement>(null);
  const newContentInput = useRef<HTMLInputElement>(null);

 

 

Object is possibly 'null'

Object is possibly 'null'

๋‹ค์Œ์œผ๋กœ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ!.. useRef์˜ ๊ฐ’์ด null์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

null ๊ฐ’์ผ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๋ง์€ ์˜ต์…”๋„ ์ฒด์ด๋‹ ์—ฐ์‚ฐ์ž๋กœ ํ•ด๊ฒฐ!..

const editTodoHandler = () => {
  //...
  if (!newTitle) {
      alert('์ œ๋ชฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
      newTitleInput?.current?.focus();} 
  //...
}

 

 

Type '...' is not assignable to type '....'

Type '...' is missing the following properties from type 'HTMLTextAreaElement' : cols, rows, textLength, wrap

 

๋ญ”๊ฐ€ ํƒ€์ž… ๊ด€๋ จ ๋ฌธ์ œ๊ฐ€ ์ž˜๋ชป๋œ ๊ฒƒ ๊ฐ™์•˜๋‹ค. conetentInput์˜ ref๋Š” ๋ญ๊ฐ€ ์ข€ ๋‹ฌ๋ž๋˜๊ฐ€ ์‹ถ์–ด์„œ ๋งˆ์šฐ์Šค๋ฅผ ๋Œ€๋ฉด ํƒ€์ž… ์ถ”๋ก ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

HTMLTextAreaElement ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์ ์šฉํ•ด์ฃผ์—ˆ๋”๋‹ˆ ์ด ๋ถ€๋ถ„์€ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค.

const newContentInput = useRef<HTMLTextAreaElement>(null);

 

 

Argument of type '...' is not assignable to parameter of type

Argument of type '(props: ThemedStyledProps<Pick<DetailedHTMLProps<HTMLAttributes, HTMLParagraphElement>, "key" | keyof HTMLAttributes<...>> & { ...; } & { ...; }, any>) => boolean' is not assignable to parameter of type

์ด ๋ถ€๋ถ„์€ ์Šคํƒ€์ผ ์ปดํฌ๋„ŒํŠธ์—์„œ props์— ๋”ฐ๋ผ css๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์ ์šฉํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค.

๊ธฐ์กด ์ฝ”๋“œ๋Š” ๋„๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž๋กœ ์ ์šฉํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„์— ์ ์šฉ์ด ๋˜์ง€ ์•Š๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋กœ ๋ฐ”๊พธ์–ด์ฃผ์—ˆ๋‹ค.

Text ์ปดํฌ๋„ŒํŠธ์—๋„ ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ null๋กœ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

 

 

 

 

Type 'boolean | undefined' is not assignable to type 'boolean'. Type 'undefined' is not assignable to type 'boolean'.

Type 'boolean | undefined' is not assignable to type 'boolean'. Type 'undefined' is not assignable to type 'boolean'.

boolean ํƒ€์ž…์ด ์•„๋‹ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ํ•ธ๋“ค๋ง์ด ํ•„์š”ํ•˜๋‹ค๋Š” ์—๋Ÿฌ๋‹ค. ๊ฐ’์ด undefined์ผ ์ˆ˜ ์žˆ์„ ๋•Œ์— ๋Œ€ํ•ด ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด ํ•ด๊ฒฐํ–ˆ๋‹ค.

return (
<TodoDetail id={todo?.id ?? '') title={todo?.title ?? ''} content={todo?.content ?? ''} isDone={todo?.isDone ??false} />);

 

 

 

 

์•„์ง ํ•ด๊ฒฐํ•ด์•ผ ํ•  ์˜ค๋ฅ˜๊ฐ€ ๋งŽ์ด ๋‚จ์•„์žˆ์ง€๋งŒ!..

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ํƒ€์ž…์— ๋Œ€ํ•œ ๊ฐ๊ฐ์„ ์กฐ๊ธˆ์”ฉ ์ตํ˜€๋‚˜๊ฐ€๋Š” ๊ณผ์ • ๊ฐ™๋‹ค!.. ๋Œ€์ฒด๋กœ null์ด๋‚˜ undefined์ผ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค๊ณ  ๋Š๊ผˆ๋‹ค. ๐Ÿซ 

728x90