[RN] react-native 'Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: number.' 에러잡기

2024. 9. 13. 14:53·React Native
반응형

🐰: 와 진짜 날 진땀빼게 한 에러였다.. 3일을 고생했다 부들..

도대체 어디가 에러인지 모르고 애꿏은 modules만 지웠다 설치했다..

아예 모든걸 지우고 react-native init부터 다시 하기도 했다..

하.................💨

 

결국 코드 한줄씩 주석처리 해보면서 svg파일이 문제인걸 찾았다.

근데 도대체 왜? 며칠 전까지만 해도 잘 되던 것들인데....

 

또 하루종일 svg와 관련된 파일들만 설치하고 난리부르스......ㅋㅋ

진짜 울기 직전에 나타난 새로운 글... 역시 사람은 검색을 잘해야 한다....

참고한 글들은 아래에 링크걸어두기로 하고..

 

발생원인

react-native 버전이 업그레이드 되고나서 발생하는 에러이다.

나의버전: "react-native": "0.75.2"

 

해결방법 ts-jest 라이브러리 사용

 

1. ts-jest 라이브러리 설치

npm install --save-dev ts-jest
# or
yarn add --dev ts-jest

 

2. jest.config.js 파일 수정

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
    '^.+\\.svg$': 'jest-svg-transformer',
  },
};

 

3. babel.config.js 파일에 plugins 추가

module.exports = {
  presets: ['module:@react-native/babel-preset'],
  plugins: [
    [
      'module-resolver',
      {
        root: ['./src'],
      },
    ],
    'react-native-reanimated/plugin',
    'react-native-svg-transformer',
  ],
};

 

4.svg 파일을 .tsx 파일로 제작

import * as React from 'react';
import { SVGProps } from 'react';
import { Svg, Path } from 'react-native-svg';

const EyeOpen = (props: SVGProps<SVGSVGElement>) => (
  <Svg xmlns="http://www.w3.org/2000/svg" fill="none" {...props}>
    <Path
      fill="#7F7F7F"
      d="M2.2 24S0 24 0 22s2.2-8 11-8 11 6 11 8-2.2 2-2.2 2H2.2ZM11 12c1.75 0 3.43-.632 4.667-1.757C16.905 9.117 17.6 7.59 17.6 6c0-1.591-.695-3.117-1.933-4.243C14.429.632 12.75 0 11 0S7.57.632 6.333 1.757C5.095 2.883 4.4 4.41 4.4 6c0 1.591.695 3.117 1.933 4.243C7.571 11.368 9.25 12 11 12Z"
    />
  </Svg>
);

export default EyeOpen;

* 여기서 주의할 점은 <svg><path>이렇게 태그의 첫 글자를 소문자로 시작하면 아래와 같은 에러가 뜨니 필히.... 대문자로 시작하세요...

 

 

참고사이트

https://stackoverflow.com/questions/58603201/jest-cannot-load-svg-file

 

Jest cannot load svg file

In my app I use React and TypeScript. I tried to run jest tests I get following error: C:\Users\e-KDKK\workspace\konrad\mikskarpety\src\images\icons\Sock.svg:1 ({"Object.<anonymous>":function(

stackoverflow.com

 

https://velog.io/@rhfovk/React-Native%EC%97%90%EC%84%9C-svg-import-%EB%AA%BB%ED%95%98%EB%8A%94-%EB%AC%B8%EC%A0%9C

 

testing-library에서 svg 인식 못하는 문제

저번에 만들었던 svg 아이콘 컴포넌트에서 test를 돌렸을 때 @testing-library/react-native에서 svg 컴포넌트를 인식하지 못하는 상황이 생겼습니다.

velog.io

 

반응형

'React Native' 카테고리의 다른 글

[RN] react-native Firebase Push Notification - iOS  (8) 2024.10.21
[RN] react-native simulator 'Hide Inspector'  (10) 2024.10.07
[RN] react-native button으로 sort 정렬하기  (0) 2024.09.06
[RN] react-native tab navigation 스크린 이동 새로고침  (0) 2024.08.28
[RN] react-native navigation params 사용하기  (0) 2024.08.28
'React Native' 카테고리의 다른 글
  • [RN] react-native Firebase Push Notification - iOS
  • [RN] react-native simulator 'Hide Inspector'
  • [RN] react-native button으로 sort 정렬하기
  • [RN] react-native tab navigation 스크린 이동 새로고침
nicee
nicee
답을 찾아가는 과정이 즐거운 프론트엔드 개발자의 기록모음🧐
    반응형
  • nicee
    나의 개발일지
    nicee
  • 글쓰기 관리
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Project01 [Chat App]
      • React Native
      • Git
      • Program Setting
      • nicee-day
      • Study
        • C++
  • 블로그 메뉴

    • 링크

      • 🌳 git키우기
      • 📚 일상기록
    • 공지사항

    • 인기 글

    • 태그

      에러해결
      error
      타입스크립트
      k직장인
      리액트
      백준코딩테스트
      직장인짤
      오블완
      React
      React-native
      k직장인일상
      IOS
      불금
      APP
      Xcode
      앱개발
      typescript
      nextjs
      리액트네이티브
      공부
      빌드에러
      next.js
      firebase
      채팅앱
      티스토리챌린지
      커피수혈
      c++
      app build
      rn
      Android
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.0
    nicee
    [RN] react-native 'Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: number.' 에러잡기
    상단으로

    티스토리툴바