Skip to content

[URH-50] useEventListener 신규 #48

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

Merged
merged 9 commits into from
Aug 22, 2024
Merged

Conversation

foresec
Copy link
Collaborator

@foresec foresec commented Aug 16, 2024

👾 Pull Request

  • 상태: 신규

1️⃣ Spec

  • 특정 이벤트를 지정된 Element에 등록하고, 해당 이벤트가 발생할 때 지정된 callback을 호출하는 커스텀 훅
  • 이벤트 리스너를 간편하게 관리할 수 있음

2️⃣ 변경 사항

  • 없음

3️⃣ 예시 코드

기본적인 RefObject에 이벤트리스너를 지정할때

import { useRef } from 'react';
import useEventListener from './hooks/useEventListener';

function App() {
  const buttonRef = useRef<HTMLButtonElement>(null);

  const handleClick = () => {
    console.log('Button clicked!');
  };

  // RefObject에 대해 click이벤트를 설정
  useEventListener("click", handleClick, buttonRef);

  return (
    <div>
      <button ref={buttonRef}>Click</button>
    </div>
  );
}

export default App;

그 외 다양한 경우들

  1. 기본 window에 바로 설정
  2. HTMLElement에 설정
  3. EventListener 기본 option 활용(capture/once/passive)
import useEventListener from './hooks/useEventListener';

function App() {
  const handleResize = () => {
    console.log(window.innerWidth, window.innerHeight);
  };

  const handleClick = (event: MouseEvent) => {
    console.log(event.clientX, event.clientY);
  };

  const handleKeydown = (event: KeyboardEvent) => {
    console.log('Key pressed:', event.key);
  };

  // 1. Window에 대해 resize 이벤트를 설정
  useEventListener('resize', handleResize);

  // 2. HTMLElement에 대해 click 이벤트를 설정
  useEventListener('click', handleClick, document.getElementById('myButton')!);

  // 3. Document에 대해 keydown 이벤트를 설정 + once 옵션 사용
  useEventListener('keydown', handleKeydown, document, { once: true });

  return (
    <div>
      <button id="myButton">Click</button>
    </div>
  );
}

export default App;

4️⃣ 관련 문서 (선택 사항)


  • 다른 레퍼런스를 참고하며 원래 오버로딩으로 구현되었었는데(216368d) Generic으로 바꾸는 과정에서 최대한 노력은 했으나, 혹시 더 좋은 아이디어가 있으시다면 알려주시면 좋을 것 같습니다.

@foresec foresec assigned bicochan and unassigned bicochan Aug 16, 2024
@foresec foresec requested a review from bicochan August 16, 2024 14:31
Copy link
Member

@bicochan bicochan left a comment

Choose a reason for hiding this comment

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

고생하셨어요 소현 님!👍🏻✨
어떤 레퍼런스를 참고하셨는지 궁금한데 링크 한 번 남겨주실 수 있으실까요?

@foresec
Copy link
Collaborator Author

foresec commented Aug 18, 2024

@bicochan useEventListener 여기 코드를 참고 했었습니다!

Copy link
Contributor

@suhyeoonn suhyeoonn left a comment

Choose a reason for hiding this comment

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

고생하셨습니다~! 특히 window에 이벤트 연결할 때 유용할 것 같네요🤩

@foresec foresec merged commit 42a687e into master Aug 22, 2024
1 check passed
@foresec foresec deleted the URH-50/use-event-listener branch August 22, 2024 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants