Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE] 존재하지 않는 element에 대한 에러를 출력하는 유틸리티 함수 제작 #150

Merged
merged 5 commits into from
Jul 31, 2024
11 changes: 11 additions & 0 deletions frontend/src/hooks/useExistentElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useEffect, RefObject } from 'react';

const useExistentElement = <T extends HTMLElement>(ref: RefObject<T>, elementName: string) => {
Copy link
Contributor

@BadaHertz52 BadaHertz52 Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용범위를 더 넓여보는 것은 어떨까요?

위 코드는 ref.current의 존재 여부를 확인하는 기능이네요
ref가 아닌 const element =document.querySelector('#contents')인 element에는 적용할 수 없어요

ref보다는 HTMLElement|Element|null|undefined 인 요소를 훅의 props로 받고 HTMLElement,Element 인지를 판단한다면
사용범위가 넓어질 것 같아요

props 타입에 대한 컨벤션

프론트 코드 컨벤션이 훅/컴포넌트 외부에서 props타입을 선언하기로 했어요. props 타입을 훅 외부로 빼고 제너릭을 사용할 수 있도록 수정해보는 것은 어떨까요?

useEffect(() => {
if (!ref.current) {
console.error(`${elementName}을/를 찾을 수 없습니다.`);
}
}, [ref, elementName]);
};

export default useExistentElement;