Po_tta_tt0
콩심콩 팥심팥 🌱
Po_tta_tt0
전체 방문자
오늘
어제
  • 분류 전체보기 (266)
    • 🐛 회고 (14)
    • 💭 생각 (2)
    • 🤸‍♀️ 내 프로젝트 (16)
      • FISH-NEWS (8)
      • MBTI 과몰입 테스트 (2)
      • twitter clonecoding with TS (4)
      • pilzagenda (2)
    • 👨‍👩‍👧‍👦 팀 프로젝트 (2)
      • 피우다 프로젝트 (0)
      • SEMO(세상의 모든 모임) (1)
      • 마음을 전하는 텃밭 (1)
      • Stackticon (0)
    • 👨‍💻 CS지식 (11)
    • ✍ 공부 (94)
      • JavaScript (21)
      • TypeScript (39)
      • Html (0)
      • CSS (2)
      • React (18)
      • NextJS (7)
      • Vue (1)
      • Python (1)
      • Django (0)
      • 개발환경 & 그 외 (2)
    • 🏄‍♀️ 코딩테스트 (99)
      • 🐍 Python (99)
    • 🐙 Git & GitHub (3)
    • 📑 오류기록 (8)
    • 📚 우리를 위한 기록 (9)
    • 수업 (3)
    • 강의 등 (2)
    • 👩‍🎓 SSAFY (0)
    • 👋 우테코 (0)

블로그 메뉴

  • 홈
  • 태그

공지사항

인기 글

태그

  • 이분탐색
  • 백준 파이썬
  • js
  • 문자열
  • DP
  • 시뮬레이션
  • React
  • Next.js
  • BFS + DP
  • 자바스크립트
  • 파이썬 다익스트라
  • 파이썬 감시 피하기
  • 백준
  • dfs
  • react-router-dom
  • 구현
  • 파이썬
  • 플로이드 워셜
  • bfs
  • 백준 숨바꼭질

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Po_tta_tt0

콩심콩 팥심팥 🌱

📑 오류기록

[ eslint ERROR ] <forwardRef> Component definition is missing display nameeslintreact/display-name

2023. 3. 13. 11:10
반응형

 

 

0. 오류 상황 발생

  • Stackticon 프로젝트를 진행하면서 발생한 문제
  • Loading.tsx 페이지 내부에 존재하는 Stacks.tsx 컴포넌트를 forwardRef로 연결하는 과정에서 발생했다.


1. 추론

에러 메세지

Component definition is missing display nameeslintreact/display-name

를 읽어보니 eslint 오류일 것이라고 생각됐다.

 

 

궁금증

  1. 왜 발생할까?
  2. eslint오류는 왜 발생할까?
  3. 어떻게 해결하는 것이 좋을까?

 

 

2. 알아보기

1. 왜 발생할까?

  • forwardRef() 함수를 호출할 때 익명 함수를 넘기면
  • 컴포넌트의 이름이 나오지 않는다 -> 이 때 발생하는 에러

 

2. eslint오류는 왜 발생할까?

eslint-plugin-react/display-name
을 읽어보니 display-name 속성은 eslint에서 디버깅을 용이하기 위한 속성이다.

 

3. 어떻게 해결하는 것이 좋을까?

 

  1. forwardRef()함수에 기명함수(이름이 있는 함수)를 넘긴다!
const Stacks = forwardRef<HTMLDivElement, StacksProps>(function funcionName(
  { color, selecteds },
  targetRef,
) {
  return (
    <Box ref={targetRef}>
      {selecteds.map((select: string) => (
        <Stack key={select} stackName={select}></Stack>
      ))}
    </Box>
  );
});

export default Stacks;

 

  1. forwardRef()의 호출 결과로 기존 컴포넌트 대체하기
function ComponentName({ color, selecteds }: StacksProps, targetRef: React.Ref<HTMLDivElement>) {
  return (
    <Box ref={targetRef}>
      {selecteds.map((select: string) => (
        <Stack key={select} stackName={select}></Stack>
      ))}
    </Box>
  );
}

export const Stacks = forwardRef(ComponentName);

 

  1. displayName 속성에 컴포넌트 이름 명시해주기
const Stacks = forwardRef<HTMLDivElement, StacksProps>(({ color, selecteds }, targetRef) => {
  return (
    <Box ref={targetRef}>
      {selecteds.map((select: string) => (
        <Stack key={select} stackName={select}></Stack>
      ))}
    </Box>
  );
});
Stacks.displayName = 'Stacks';

export default Stacks;

저는 3번의 방법으로 해결했습니다!

 

요약

  1. forwardRef 함수를 호출할 때 익명 함수를 넘기면, 브라우저에서 컴포넌트의 이름이 나오지 않는다
    • eslint display-name(https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/display-name.md) 속성을 통한 디버깅을 시도하거나
    • TypeScript 사용 시 에러가 발생한다
  2. 해결 방법
    • forwardRef()에 기명 함수를 넘기거나
    • forwardRef()의 호출 결과로 기존 컴포넌트를 대체하거나
    • displayName에 컴포넌트 이름을 명시해준다
반응형
저작자표시 (새창열림)

'📑 오류기록' 카테고리의 다른 글

[husky] Checking errors when build projectstdin is not a tty  (1) 2023.06.09
[ axios && jest ERROR] Cannot use import statement outside a module  (0) 2023.01.05
[ FireBase ERROR ] Argument of type 'DocumentData' is not assignable to parameter of type 'Topic'.  (0) 2022.12.27
[ React 18 ] Hot reloading 문제. You are importing hydrateRoot from "react-dom" which is not supported. You should instead import it from "react-dom/client"  (1) 2022.03.29
npm start / yarn start가 안될 때. 오류코드 : error Command failed with exit code 1.  (0) 2022.03.07
    '📑 오류기록' 카테고리의 다른 글
    • [husky] Checking errors when build projectstdin is not a tty
    • [ axios && jest ERROR] Cannot use import statement outside a module
    • [ FireBase ERROR ] Argument of type 'DocumentData' is not assignable to parameter of type 'Topic'.
    • [ React 18 ] Hot reloading 문제. You are importing hydrateRoot from "react-dom" which is not supported. You should instead import it from "react-dom/client"
    Po_tta_tt0
    Po_tta_tt0
    감자의 코딩하는 블로그 콩심은데 콩나고 팥심은데 팥난다

    티스토리툴바