[RN] Warning: A props object containing a "key" prop is being spread into JSX 에러

2024. 10. 15. 17:52·React Native

개요


프로젝트를 진행하던 중 의문의 에러가 발생했다.
React-Native의 Material-Top-Tab-Navigation이 있는 컴포넌트에 들어가면 해당 에러가 발생했다.
 

경고(에러)
콘솔

단순한 경고라고 생각하고 넘어갈 수도 있지만 콘솔에는 ERROR로 출력되는 모습이다.
 
나중에 앱 빌드할 때 문제가 될 수 있으니 해결하고 넘어가려 했다.


해결 방법


24년 5월 8일 공식 라이브러리의 issue에서 에러 해결법을 찾을 수 있었다.
 
https://github.com/react-navigation/react-navigation/issues/11989

A props object containing a "key" prop is being spread into JSX · Issue #11989 · react-navigation/react-navigation

Current behavior Every time I load my app, after updating a bunch of my packages (of note: react-native, expo), I get this error: Warning: A props object containing a "key" prop is being spread int...

github.com

 
해당 페이지에서 에러 해결 패치 파일 내용을 공유중이었다.
 
패치 파일은 다음과 같다.

diff --git a/src/TabBar.tsx b/src/TabBar.tsx
index e8d0b4c8dcbbe779fcd304f483d2d91c2d5e8dde..203adc927db153df3f8472d4ec67346e1cd7405b 100644
--- a/src/TabBar.tsx
+++ b/src/TabBar.tsx
@@ -364,8 +364,7 @@ export function TabBar<T extends Route>({
 
   const renderItem = React.useCallback(
     ({ item: route, index }: ListRenderItemInfo<T>) => {
-      const props: TabBarItemProps<T> & { key: string } = {
-        key: route.key,
+      const props: TabBarItemProps<T> = {
         position: position,
         route: route,
         navigationState: navigationState,
@@ -446,9 +445,9 @@ export function TabBar<T extends Route>({
         <>
           {gap > 0 && index > 0 ? <Separator width={gap} /> : null}
           {renderTabBarItem ? (
-            renderTabBarItem(props)
+            renderTabBarItem({key: route.key, ...props})
           ) : (
-            <TabBarItem {...props} />
+            <TabBarItem key={route.key} {...props} />
           )}
         </>
       );

 
해당 패치 파일을 그대로 적용해도 되고, 직접 해당 파일로 가서 수정해도 된다.
 
node_modules/react-native-tab-view/src/TabBar.tsx 로 이동한다.
 
367, 368 line을 다음과 같이 수정한다.
 

const props: TabBarItemProps<T> = {
//key: route.key, 이거 주석처리 하거나 삭제

 
그 후 renderItem 함수의 return값을 다음과 같이 수정한다.

<>
       {gap > 0 && index > 0 ? <Separator width={gap} /> : null}
       {renderTabBarItem ? (
         renderTabBarItem({key: route.key, ...props})
       ) : (
         <TabBarItem key={route.key} {...props} />
       )}
</>

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

[RN] React Native cli Android에서 react-native-vector-icons 라이브러리 사용하기  (2) 2024.10.13
'React Native' 카테고리의 다른 글
  • [RN] React Native cli Android에서 react-native-vector-icons 라이브러리 사용하기
MaKa_
MaKa_
이게 왜안될까요
  • MaKa_
    벌레 잡는 사람
    MaKa_
  • 전체
    오늘
    어제
    • 공부 (45)
      • CS (18)
        • 자료구조(data structure) (7)
        • 알고리즘(Algorithm) (6)
      • React (6)
      • React Native (2)
      • Next.js (5)
      • Backend (2)
      • 이야기 (3)
        • 회고 (2)
        • 일상 (1)
      • 기타 (7)
      • 알고리즘 문제 (2)
        • 백준 (0)
        • 프로그래머스 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 깃허브
  • 공지사항

  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
MaKa_
[RN] Warning: A props object containing a "key" prop is being spread into JSX 에러
상단으로

티스토리툴바