| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- Next13
- 고급안내서
- Render Props
- background tab
- Programmers
- React 고급안내서
- React 18
- react hook
- Nextjs React 18
- RTK Query
- react
- hook
- react-helmet
- Javascript
- context
- codingtest
- getUTCDate
- background: url
- Nextjs
- React API 참고서
- next13 head
- React 공식문서
- CSS
- background setInterval
- background setttimeout
- React18
- React 고급 안내서
- React 18 Nextjs
- Babel
- notFound()
Archives
- Today
- Total
akjfal
Cannot invoke an object which is possibly 'undefined' 본문
에러 발생 코드
//useAlert.ts
return [{sentence, isOpen}, {open, close}]
//useLogin.ts
const [alertData, alertAction] = useAlert();
...
alertAction.open("로그인 성공"); // error 발생
발생 에러
Cannot invoke an object which is possibly 'undefined'.ts(2722)
Cannot invoke an object which is possibly 'undefined'.ts(2722)
const alertAction: {
sentence: any;
isOpen: any;
open?: undefined;
close?: undefined;
} | {
open: (data: any) => void;
close: () => void;
sentence?: undefined;
isOpen?: undefined;
}
에러 설명과 검색을 해봤을때 내가 지정해준 변수가다른 변수를 가르킬 수 있기때문에 타입 지정에러가 발생한다고한다.
해결
useAlert.ts의 return값을 변경하지 않고 해결해보려고했으나 실패했다...
// useAlert.ts
return {
alertState: { sentence, isOpen },
alertAction: {
open,
close,
},
};
// useLogin.ts
const { alertAction } = useAlert();
이런식으로 정의를 해서 해결했다...
Comments