Giter Club home page Giter Club logo

Comments (1)

bluepebble25 avatar bluepebble25 commented on June 12, 2024

겪은 문제 (타입 지정이 어렵다!)

  • hook을 만들 때 ref, handler, event listener 함수 등 type 지정이 어려웠다.

해결방법

  • ref는 React.RefObject<Ref를 붙인 엘레먼트의 타입>으로 타입을 지정한다. modal에 붙일 건데 왜 HTMLDivElement로 지정하지 않았냐면, hook은 재사용을 위한 것이기 때문에 HTMLElement로 충분하다고 생각했기 때문이다.
  • React의 이벤트 함수는 (ex-onClick)는 event의 타입을 React.MouseEvent처럼 지정하면 됐는데, vanilla js의 이벤트를 등록할 때 하던대로 e: React.MouseEvent로 타입을 지정했더니 에러가 떴다. vanilla js 이벤트함수의 이벤트는 타입을 Event로 주면 된다.
  • e에 타입 지정을 해도 e.target에 타입 에러가 뜬다. event에도 타입 지정을 해주고, target도 따로 변수로 빼서 HTMLElement 계열로 타입 지정을 해주면 된다.
// useOnClickOutside.ts
import React, { useEffect } from 'react';

const useOnClickOutside = (
  ref: React.RefObject<HTMLElement>,
  handler: Function
) => {
  useEffect(() => {
    const listener = (e: Event) => {
      const target = e.target as HTMLElement;
      if (ref.current && !ref.current.contains(target)) {
        handler();
      }
    };

    document.addEventListener('mousedown', listener);
    document.addEventListener('touchstart', listener);

    return () => {
      document.removeEventListener('mousedown', listener);
      document.addEventListener('touchstart', listener);
    };
  }, [ref, handler]);
};

export default useOnClickOutside;
// CardDetailPage.tsx 중 일부
const modalRef = useRef<HTMLDivElement>(null);
const navigate = useNavigate();
const goBack = () => {
  navigate('/cards');
};
useOnClickOutside(modalRef, goBack);

from phoca-review.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.