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

Today What I learned?

์–ด์ œ์— ์ด์–ด ๋ฆฌ๋•์Šค ํˆดํ‚ท ํˆฌ๋‘๋ฆฌ์ŠคํŠธ๋ฅผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•˜๋ฉฐ ํ•ด๊ฒฐํ•œ ๋งŽ์€ ์˜ค๋ฅ˜๋“ค์„ ๊ธฐ๋กํ•ด ๋ณธ๋‹ค..!!
์–ด์ฐŒ์–ด์ฐŒ ๋ชจ๋“  ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ  ์ •์ƒ ์ž‘๋™์ด ๋˜๊ณ  ์žˆ๋‹ค.. ๐Ÿฅบ

 

 

 

๐Ÿšง ๋ฒ„ํŠผ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜ ํ•ด๊ฒฐ

Type '' is missing the following properites from type ~~

Type '' is missing the following properites from type ~~

์•”๋งŒ ๋ด๋„ ๋ฒ„ํŠผ ์ปดํฌ๋„ŒํŠธ์— ๋‹ค๋ฅธ ํ•จ์ˆ˜ props๊ฐ€ ์—†๋‹ค๋Š” ์–˜๊ธฐ ๊ฐ™๋‹ค.

๊ธฐ์กด ์ฝ”๋“œ๋Š” ์ € ํ•จ์ˆ˜๋“ค ์ค‘ ํ•˜๋‚˜๋งŒ ๋„ฃ์–ด์ฃผ์–ด ๋ฒ„ํŠผ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ณตํ†ต์œผ๋กœ ์“ฐ๊ณ  ์žˆ๋Š” ์ƒํƒœ์˜€๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ๋ชจ๋“  ํ•จ์ˆ˜ props๋ฅผ ๋ฐ›์„ ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์„ ํƒ์ ์œผ๋กœ ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋‹ˆ ํƒ€์ž… ์†์„ฑ์— ์˜ต์…”๋„ ์ฒด์ด๋‹ ์—ฐ์‚ฐ์ž๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋”๋‹ˆ 1์ฐจ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค.

 

 

Cannot invoke an object which is possibly 'undefined'

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋˜๋‹ˆ ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”๋ฐ (..)

ํด๋ฆญ ์‹œ ํƒ€์ž…์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ํ•จ์ˆ˜๊ฐ€ ๋™์ž‘ํ•˜๋„๋ก ํ•˜๋Š” ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด๊ฐ’๋“ค์—๊ฒŒ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ฒผ๋‹ค.

Cannot invoke an object which is possibly 'undefined'

ํ•จ์ˆ˜๊ฐ€ undefined ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ!.. ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์—๊ฒŒ๋„ ์˜ต์…”๋„ ์ฒด์ด๋‹ ์—ฐ์‚ฐ์ž๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

์ด๋Ÿฐ ์‹์œผ๋กœ!.. ํ•จ์ˆ˜์—๊ฒŒ๋„ ์˜ต์…”๋„ ์ฒด์ด๋‹ ์—ฐ์‚ฐ์ž(?.)๋ฅผ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ฒ˜์Œ ์•Œ์•˜๋‹ค. ๐Ÿค”

์ด๋ ‡๊ฒŒ 2์ฐจ ํ•ด๊ฒฐ์ด ๋˜์—ˆ์œผ๋‚˜....

 

 

Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.

์ด๋ฒˆ์—” ๋งค๊ฐœ๋ณ€์ˆ˜ id ์ชฝ์˜ ์—๋Ÿฌ๋‹ค.. ใ…Žใ…Ž

id์˜ ๊ฒฝ์šฐ์—๋Š” ์™„๋ฃŒ ํ† ๊ธ€๊ณผ ์‚ญ์ œ ์‹œ์—๋งŒ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„ ํƒ์ ์œผ๋กœ ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋Š” ๊ฐ’์ด๋ผ ์˜ต์…”๋„ ์ฒด์ด๋‹์„ ๋„ฃ์–ด์ฃผ์—ˆ๋”๋‹ˆ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

 

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.  
Type 'undefined' is not assignable to type 'string'.

์™œ๋ƒ!.. ์•„์ด๋””๊ฐ€ ์—†๋‹ค๋ฉด undefined์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ํ•ธ๋“ค๋ง์ด ํ•„์š”ํ–ˆ๋‹ค.

์•„์ด๋””๊ฐ€ undefined์ผ ๊ฒฝ์šฐ์—๋Š” ๋นˆ ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์ฃผ๋„๋ก currentId ๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

์ด๋กœ์จ ๋ฒ„ํŠผ ์ปดํฌ๋„ŒํŠธ ๊ด€๋ จ ์˜ค๋ฅ˜๋“ค ํ•ด๊ฒฐ.. ๐Ÿ˜‡

 

 

 

๐Ÿšง ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ useRef์˜ ref๊ฐ’์„ props๋กœ ๋„˜๊ธฐ๋ฉฐ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜

๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ ์ตœ์ƒ๋‹จ์—์„œ current.focus() ๋ฅผ ์จ์ฃผ๊ธฐ ์œ„ํ•ด useRef ํ›…์„ ์‚ฌ์šฉํ–ˆ๊ณ ,

์ด๊ฑด ์ง€์—ญ๋ณ€์ˆ˜๋ฅผ ์œ„ํ•ด ์“ฐ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ DOM ์กฐ์ž‘์„ ์œ„ํ•œ ๊ฒƒ์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์— <HTMLInputElement> ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

์ด๊ฒƒ์„ ์Šคํƒ€์ผ ์ปดํฌ๋„ŒํŠธ๋กœ ๋งŒ๋“  Inputs ์—๊ฒŒ props๋กœ ์ „๋‹ฌํ•  ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์—ˆ๋‹ค.

 const titleInput = useRef<HTMLInputElement | null>(null);
 const contentInput = useRef<HTMLInputElement | null>(null);
  ...

 <form onSubmit={submitHandler}>
        <Inputs
          ...
          ref={titleInput}
        />
        <Inputs
          label={'๋‚ด์šฉ'}
          id={'contents'}
          value={content}
          onChange={changeContentHandler}
          ref={contentInput} // ์˜ค๋ฅ˜ ๋ฐœ์ƒ์ง€์ !! 
        />
        <Button type="submit">์ถ”๊ฐ€</Button>
      </form>

 

๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๋Š” ์ด๋žฌ๋‹ค.

Type 'RefObject<HTMLInputElement>' is not assignable to type '(inputElement: HTMLInputElement) => void'.  
  Type 'RefObject<HTMLInputElement>' provides no match for the signature '(inputElement: HTMLInputElement): void'.

 

 

์•„๋ž˜๋Š” Input.tsx ์ธ๋ฐ ์—ฌ๊ธฐ์„œ ๋ฐ›์•„์˜จ props์˜ ํƒ€์ž…์„ ์ง€์ •ํ•  ๋•Œ ref์˜ ํƒ€์ž…์ด ๋ญ”๊ฐ€ ์•ˆ ๋งž๋Š”๋‹ค๋Š” ๋Š๋‚Œ์ด ๋“ค์—ˆ๋‹ค. ๐Ÿค”
์‹ค์ œ๋กœ๋„ ์ด ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์—ˆ๊ณ ..

interface InputTypes {
  //...
  ref: HTMLInputElement | null;
}

const Inputs = ({ label, id, value, onChange, ref }: InputTypes) => {
  return (
    <>
      <Label htmlFor={id}>{label}</Label>
      <Input
        //....
        ref={ref} // ์˜ค๋ฅ˜ ๋ฐœ์ƒ์ง€์ !! 
      />
    </>
  );

 

์ฐพ์•„๋ณด๋‹ˆ ref์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ForwardedRef ๋ผ๋Š” ํƒ€์ž…์„ ๋„ฃ์–ด์ค€๋‹ค๊ณ  ํ•œ๋‹ค.

React.ForwardedRef<HTMLํƒœ๊ทธํƒ€์ž…>

 

 

๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” input ํƒœ๊ทธ์— ๋Œ€ํ•œ ref์˜€๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋„ฃ์–ด์ฃผ์–ด ํ•ด๊ฒฐํ–ˆ๋‹ค.

  ref: ForwardedRef<HTMLInputElement>;

 

 

 

๋งˆ์ง€๋ง‰์œผ๋กœ index ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด์ฃผ๋‹ค๋ณด๋‹ˆ dom ์„ ํƒํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด์ฃผ์–ด ํ•ด๊ฒฐํ–ˆ๋‹ค!.. 

 

 

์ด๋ ‡๊ฒŒ 40์—ฌ ๊ฐœ ๋˜์–ด๊ฐ€๋˜ ์—๋Ÿฌ๋“ค์„ ๋ชจ๋‘ ํ•ด๊ฒฐํ•˜๊ณ  ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์™„๋ฃŒ..

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๋งŒ ์ตํžŒ ์ฑ„๋กœ ๋ฌด์ž‘์ • ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•˜๋‹ค ๋ณด๋‹ˆ ์ •๋ง ๋งŽ์€ ์—๋Ÿฌ๋“ค์„ ๊ฒช์—ˆ๋‹ค. ๐Ÿ˜‡

์ง์ ‘ ๋ถ€๋”ชํžˆ๋‹ค ๋ณด๋‹ˆ ๋˜ ๋งŽ์€ ๊ฒƒ๋“ค์„ ์–ป๊ฒŒ ๋˜์—ˆ๊ณ  ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์— ๋Œ€ํ•ด์„œ ์ข€ ์•Œ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๋‹ค.

๋˜ ๋‹ค๋ฅธ ๊ฒƒ๋„ ํ•œ๋ฒˆ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๋Š” ๊ฒƒ์„ ์‹œ๋„ํ•ด์•ผ๊ฒ ๋‹ค!..

 

 

 

 

 

 

์ฐธ๊ณ ๊ธ€

https://bobbyhadz.com/blog/typescript-cannot-invoke-an-object-which-is-possibly-undefined

https://stackoverflow.com/questions/73015696/whats-the-difference-between-reacts-forwardedref-and-refobject

728x90