Notice
Recent Posts
Recent Comments
Link
250x250
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 그누보드반응형
- Git clone
- minwidth
- 웹아이콘
- 단어단위로떨어지기
- git lab clone
- node설치
- react npm install
- fontawesome
- 글자들여쓰기
- maxwidth
- window 정책변경
- 정적객체
- googleicon
- npm start
- MediaQuery
- owlcarousel
- npm install
- 이미지반응형
- 동적객체
- 의존성문제
- vscode git clone
- slickslider
- legacy-peer
- npm install 문제
- XEIcon
- 아이콘사용법
- 플러그인
- package.json
- node 오류
Archives
- Today
- Total
어쩌다 알게 된 ƪ(•̃͡•̃͡ ƪ
🟦 [React] Uncaught TypeError: Cannot read properties of undefined (reading 'filter') 본문
개발/🟦 React
🟦 [React] Uncaught TypeError: Cannot read properties of undefined (reading 'filter')
비니_ 2025. 3. 23. 12:08728x90
🚫 에러 내용 🚫
Uncaught TypeError: Cannot read properties of undefined (reading 'filter')

const filteredData = {
...tabData,
[tabDataKey]: tabData[tabDataKey].filter(item =>
JSON.stringify(item).toLowerCase().includes(keyword)
)
}
❓ 오류난 이유❓
=> tabData[tabDataKey]가 undefined이거나 null일 때 filter 메서드를 호출하면 오류가 발생
=> tabData[tabDataKey]가 undefined이면 그 위에서 filter()를 호출할 수 없어서 타입 오류가 발생
📚 해결방법 📚
=> tabData[tabDataKey] && tabData[tabDataKey].filter()
&&를 붙이고 한번 더 해줬더니 에러내용이 없어졌다.
const filteredData = {
...tabData,
[tabDataKey]: tabData[tabDataKey] && tabData[tabDataKey].filter(item =>
JSON.stringify(item).toLowerCase().includes(keyword)
)
}
❓ 해결된 이유❓
=> tabData[tabDataKey]가 undefined이거나 null이면 && 뒤의 filter()를 실행하지 않음
=> filter()는 undefined가 아니라 false처럼 처리되고, filteredData는 undefined 대신에 false가 들어감
=> 옵셔널 체이닝과 비슷하게 동작하지만, 조금 더 명시적인 방식으로 undefined 체크
📚 해결방법 2 📚
=> 옵셔널 체이닝 사용
const filteredData = {
...tabData,
[tabDataKey]: tabData[tabDataKey]?.filter(item =>
JSON.stringify(item).toLowerCase().includes(keyword)
)
}
=> tabData[tabDataKey]가 undefined나 null일 경우 filter()가 자동으로 실행되지 않게 되고, filteredData는 undefined나 null 대신 [](빈 배열)을 반환
생각하니 에러는 안나지만 내가 구현하고 싶은 부분은 나오지 않고 있따..
다시 짜야지..
그치만 에러나는 이유는 알았다..!

728x90
'개발 > 🟦 React' 카테고리의 다른 글
| 🟦 [React] 검색 기능 구현해보기 2 (0) | 2025.03.24 |
|---|---|
| 🟦 [React] 검색 기능 구현해보기 (0) | 2025.03.23 |
| 🟦 [React] onClick={함수}, {함수()}, {() => 함수}, {() => 함수()} 차이 (0) | 2025.03.20 |
| 🟦 [React] 텍스트 불러올 때 <br /> 사용하기 => <React.Fragment> 이용 (0) | 2025.03.20 |
| 🟦 [React] 배열 반환할 때, 타입이 달라도 무조건적으로 배열 가져오기 (0) | 2025.03.19 |
Comments
