检测React组件外部的单击

检测React组件外部的单击

我正在寻找一种方法来检测是否在组件外发生了单击事件,如本文所述。jQuery nearest()用于查看click事件中的目标是否具有dom元素作为其父元素之一。如果匹配,则click事件属于其中一个子项,因此不被视为在组件之外。

所以在我的组件中,我想将一个单击处理程序附加到窗口。当处理程序触发时,我需要将目标与我的组件的dom子项进行比较。

click事件包含“path”之类的属性,它似乎保存了事件所经过的dom路径。我不确定要比较什么或如何最好地遍历它,我认为有人必须已经把它放在一个聪明的实用功能中......不是吗?


慕妹3242003
浏览 715回答 3
3回答

HUH函数

以下解决方案使用ES6并遵循绑定的最佳实践以及通过方法设置ref。要看到它的实际效果:课程实施钩子实施课程实施:import&nbsp;React,&nbsp;{&nbsp;Component&nbsp;}&nbsp;from&nbsp;'react';/** &nbsp;*&nbsp;Component&nbsp;that&nbsp;alerts&nbsp;if&nbsp;you&nbsp;click&nbsp;outside&nbsp;of&nbsp;it &nbsp;*/export&nbsp;default&nbsp;class&nbsp;OutsideAlerter&nbsp;extends&nbsp;Component&nbsp;{ &nbsp;&nbsp;constructor(props)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;super(props); &nbsp;&nbsp;&nbsp;&nbsp;this.setWrapperRef&nbsp;=&nbsp;this.setWrapperRef.bind(this); &nbsp;&nbsp;&nbsp;&nbsp;this.handleClickOutside&nbsp;=&nbsp;this.handleClickOutside.bind(this); &nbsp;&nbsp;} &nbsp;&nbsp;componentDidMount()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;document.addEventListener('mousedown',&nbsp;this.handleClickOutside); &nbsp;&nbsp;} &nbsp;&nbsp;componentWillUnmount()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;document.removeEventListener('mousedown',&nbsp;this.handleClickOutside); &nbsp;&nbsp;} &nbsp;&nbsp;/** &nbsp;&nbsp;&nbsp;*&nbsp;Set&nbsp;the&nbsp;wrapper&nbsp;ref &nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;setWrapperRef(node)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;this.wrapperRef&nbsp;=&nbsp;node; &nbsp;&nbsp;} &nbsp;&nbsp;/** &nbsp;&nbsp;&nbsp;*&nbsp;Alert&nbsp;if&nbsp;clicked&nbsp;on&nbsp;outside&nbsp;of&nbsp;element &nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;handleClickOutside(event)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(this.wrapperRef&nbsp;&&&nbsp;!this.wrapperRef.contains(event.target))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert('You&nbsp;clicked&nbsp;outside&nbsp;of&nbsp;me!'); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} &nbsp;&nbsp;render()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<div&nbsp;ref={this.setWrapperRef}>{this.props.children}</div>; &nbsp;&nbsp;}}OutsideAlerter.propTypes&nbsp;=&nbsp;{ &nbsp;&nbsp;children:&nbsp;PropTypes.element.isRequired,};钩子实施:import&nbsp;React,&nbsp;{&nbsp;useRef,&nbsp;useEffect&nbsp;}&nbsp;from&nbsp;"react";/** &nbsp;*&nbsp;Hook&nbsp;that&nbsp;alerts&nbsp;clicks&nbsp;outside&nbsp;of&nbsp;the&nbsp;passed&nbsp;ref &nbsp;*/function&nbsp;useOutsideAlerter(ref)&nbsp;{ &nbsp;&nbsp;/** &nbsp;&nbsp;&nbsp;*&nbsp;Alert&nbsp;if&nbsp;clicked&nbsp;on&nbsp;outside&nbsp;of&nbsp;element &nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;function&nbsp;handleClickOutside(event)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(ref.current&nbsp;&&&nbsp;!ref.current.contains(event.target))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("You&nbsp;clicked&nbsp;outside&nbsp;of&nbsp;me!"); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} &nbsp;&nbsp;useEffect(()&nbsp;=>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Bind&nbsp;the&nbsp;event&nbsp;listener &nbsp;&nbsp;&nbsp;&nbsp;document.addEventListener("mousedown",&nbsp;handleClickOutside); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;()&nbsp;=>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Unbind&nbsp;the&nbsp;event&nbsp;listener&nbsp;on&nbsp;clean&nbsp;up &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.removeEventListener("mousedown",&nbsp;handleClickOutside); &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;});}/** &nbsp;*&nbsp;Component&nbsp;that&nbsp;alerts&nbsp;if&nbsp;you&nbsp;click&nbsp;outside&nbsp;of&nbsp;it &nbsp;*/export&nbsp;default&nbsp;function&nbsp;OutsideAlerter(props)&nbsp;{ &nbsp;&nbsp;const&nbsp;wrapperRef&nbsp;=&nbsp;useRef(null); &nbsp;&nbsp;useOutsideAlerter(wrapperRef); &nbsp;&nbsp;return&nbsp;<div&nbsp;ref={wrapperRef}>{props.children}</div>;}

蛊毒传说

UseOnClickOutside Hook - React 16.8 +创建一个通用useOnOutsideClick函数export&nbsp;const&nbsp;useOnOutsideClick&nbsp;=&nbsp;handleOutsideClick&nbsp;=>&nbsp;{ &nbsp;&nbsp;const&nbsp;innerBorderRef&nbsp;=&nbsp;useRef(); &nbsp;&nbsp;const&nbsp;onClick&nbsp;=&nbsp;event&nbsp;=>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;innerBorderRef.current&nbsp;&& &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!innerBorderRef.current.contains(event.target) &nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handleOutsideClick(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}; &nbsp;&nbsp;useMountEffect(()&nbsp;=>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;document.addEventListener("click",&nbsp;onClick,&nbsp;true); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;()&nbsp;=>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.removeEventListener("click",&nbsp;onClick,&nbsp;true); &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;}); &nbsp;&nbsp;return&nbsp;{&nbsp;innerBorderRef&nbsp;};};const&nbsp;useMountEffect&nbsp;=&nbsp;fun&nbsp;=>&nbsp;useEffect(fun,&nbsp;[]);然后在任何功能组件中使用钩子。const&nbsp;OutsideClickDemo&nbsp;=&nbsp;({&nbsp;currentMode,&nbsp;changeContactAppMode&nbsp;})&nbsp;=>&nbsp;{ &nbsp;&nbsp;const&nbsp;[open,&nbsp;setOpen]&nbsp;=&nbsp;useState(false); &nbsp;&nbsp;const&nbsp;{&nbsp;innerBorderRef&nbsp;}&nbsp;=&nbsp;useOnOutsideClick(()&nbsp;=>&nbsp;setOpen(false)); &nbsp;&nbsp;return&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;<div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button&nbsp;onClick={()&nbsp;=>&nbsp;setOpen(true)}>open</button> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{open&nbsp;&&&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div&nbsp;ref={innerBorderRef}> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SomeChild/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)} &nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp;&nbsp;);};链接到演示
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript