📜  在 div 的外部单击时关闭 div 弹出窗口 - 无论代码示例

📅  最后修改于: 2022-03-11 14:55:32.599000             🧑  作者: Mango

代码示例1
const useOutsideAlerter = (ref: any) => {
    useEffect(() => {
        function handleClickOutside(event: any) {
        if (ref.current && !ref.current.contains(event.target)) {
          setOpen(0);
        }
      }
      document.addEventListener("mousedown", handleClickOutside);
      return () => {
       
        document.removeEventListener("mousedown", handleClickOutside);
      };
    }, [ref]);
  };
  const wrapperRef = useRef(null);
  useOutsideAlerter(wrapperRef);
      

suppose this div works as a popup and i want to close this popup when clicking outside of the div