弹框外部点击关闭

import React, { useLayoutEffect, useRef } from 'react'

export default function ceshi() {
  const as = useRef(null)
  const handleCl =()=>{
    window.addEventListener("click", (e) => {
      
      if (e.target != as.current) {
        console.log(e.target);
      }
    });
  }
  useLayoutEffect(() => {
    handleCl()
  }, []);
  return (
    <div>
      <div style={{width:'100px',height:'100px',border:"1px solid red"}}></div>
      <button>点击</button>
    </div>
  )
}

  

posted @ 2023-05-17 09:05  zjxgdq  阅读(1)  评论(0编辑  收藏  举报