반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- FGT
- reactStrictMode
- undefine
- Git
- 게임용어
- OBT
- 깃 브랜치 삭제
- nextjs
- 2번실행
- 알파테스트
- delete branch
- useEffect
- typescript
- console.log
- 원격
- reactjs
- ts2322
- onchange
- useRef에러
- react
- changeEvent
- useref
- 로컬
- MutableRefObject
- StrictMode
- KeyboardEvent
- react에러
- branch
- onkeypress
- event type
Archives
- Today
- Total
min.js
react useRef 에러 TS2322 MutableRefObject<undefined> 본문
반응형
react useRef 사용하면서 자주 겪는 에러 TS2322 MutableRefObject<undefined> 해결방법
useRef를 사용하고 나니 아래와 같은 에러와 함께 빨간줄이 그어졌다..
Type 'MutableRefObject<undefined>' is not assignable to type 'LegacyRef<HTMLDivElement> | undefined'.
Type 'MutableRefObject<undefined>' is not assignable to type 'RefObject<HTMLDivElement>'.
Types of property 'current' are incompatible.
Type 'undefined' is not assignable to type 'HTMLDivElement | null'.ts(2322)
index.d.ts(125, 9): The expected type comes from property 'ref' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>,
찾아보니 useRef 선언시에 null을 빼먹어서 생기는 에러였다.
// ❌ ❌ ❌
const boxRef = useRef();
// ✅ ✅ ✅
const boxRef = useRef(null);
해결 완료!!
반응형
'React' 카테고리의 다른 글
react useEffect 2번 실행되는 문제 (0) | 2024.01.30 |
---|---|
[React] 리액트 'Enter' 키 이벤트 enter key event typescript (0) | 2022.04.12 |
Comments