Component definition is missing display name 에러
by 담담이담const Button = memo(({ text, onClick }) => { console.log('Button', text, 're-rendering 😜'); const result = useMemo(() => calculateSomething(), []); return ( <button onClick={onClick} style={{ backgroundColor: 'black', color: 'white', borderRadius: '20px', margin: '0.4rem', }} > {`${text} ${result}`} </button> ); });
eslint에서 발생하는 에러로
display-name은 디버깅을 용이하게 하기 위한 속성이다.
이는 memo의 콜백함수로 익명함수를 넘겨줬기 때문에 발생한다.
해결방법
1) 해당 컴포넌트의 displayName 속성에 이름 적어주기
const Button = memo(({ text, onClick }) => { console.log('Button', text, 're-rendering 😜'); const result = useMemo(() => calculateSomething(), []); return ( <button onClick={onClick} style={{ backgroundColor: 'black', color: 'white', borderRadius: '20px', margin: '0.4rem', }} > {`${text} ${result}`} </button> ); }); Button.displayName = 'Button';
2) 익명 함수를 기명함수로 바꾸기
const Button = memo(function ButtonName({ text, onClick }) { console.log('Button', text, 're-rendering 😜'); const result = useMemo(() => calculateSomething(), []); return ( <button onClick={onClick} style={{ backgroundColor: 'black', color: 'white', borderRadius: '20px', margin: '0.4rem', }} > {`${text} ${result}`} </button> ); });
3) memo의 결과를 다시 변수에 넣기
const Button1 = ({ text, onClick }) => { console.log('Button', text, 're-rendering 😜'); const result = useMemo(() => calculateSomething(), []); return ( <button onClick={onClick} style={{ backgroundColor: 'black', color: 'white', borderRadius: '20px', margin: '0.4rem', }} > {`${text} ${result}`} </button> ); }; const Button = memo(Button1);

블로그의 정보
유명한 담벼락
담담이담