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);
'프로젝트' 카테고리의 다른 글
CI를 위한 GitHub Action 설정하기 (0) | 2024.01.20 |
---|---|
NextJS 프로젝트 초기 설정 (feat 리액트 쿼리, 리덕스, 타입스크립트) (0) | 2024.01.20 |
nvm과 npm 그리고 npx의 차이 (0) | 2024.01.19 |
모달 바깥 부분이나 ESC 키를 누르면 모달이 닫히는 기능 구현 (1) | 2024.01.03 |
React에서 onBlur를 활용한 케밥 옵션 닫기 기능 구현 (1) | 2024.01.03 |
블로그의 정보
유명한 담벼락
담담이담