Detect click outside React component

后端 未结 30 1534
日久生厌
日久生厌 2020-11-22 13:54

I\'m looking for a way to detect if a click event happened outside of a component, as described in this article. jQuery closest() is used to see if the target from a click e

30条回答
  •  野性不改
    2020-11-22 14:14

    you can you a simple way to solve your problem , i show you :

    ....

    const [dropDwonStatus , setDropDownStatus] = useState(false)
    
    const openCloseDropDown = () =>{
     setDropDownStatus(prev => !prev)
    }
    
    const closeDropDown = ()=> {
     if(dropDwonStatus){
       setDropDownStatus(false)
     }
    }
    .
    .
    .
    
     
    
    

    this works for me , good luck ;)

提交回复
热议问题