简体   繁体   English

如何检查用户是否在当前组件内单击了?

[英]How do I check if the user clicked inside the current component?

I have a component called Dialog , in which I attach an event listener on mouse clicks on the window object. 我有一个名为Dialog的组件,在其中我通过鼠标点击window对象附加一个事件监听window

componentDidMount() {
    document.addEventListener('click', this.handleClick);
}

componentWillUnmount() {
    document.removeEventListener('click', this.handleClick);
}

How can I detect (in the handleClick function) whether a click has been fired inside the component or outside? 如何检测(在handleClick函数中)是否在组件内部或外部触发了单击? Note that this dialog contains different elements and child components. 请注意,此对话框包含不同的元素和子组件。

parent.contains(child) is your friend. parent.contains(child)是你的朋友。 This solution using refs might not be perfect, but simply using this does not work as it's not a proper DOM node. 该解决方案利用refs可能不是完美的,但简单地使用this行不通,因为它不是一个适当的DOM节点。 I'm using React 15 here, so keep in mind that in earlier versions you'd have to use .getDOMNode() on the parent. 我在这里使用React 15,所以请记住,在早期版本中,您必须在父级上使用.getDOMNode()

 class Dialog extends React.Component { constructor() { super(); this.handleClick = this.handleClick.bind(this); } componentDidMount() { document.addEventListener('click', this.handleClick); } componentWillUnmount() { document.removeEventListener('click', this.handleClick); } handleClick(e) { if (this.node.contains(e.target)) { console.log('You clicked INSIDE the component.') } else { console.log('You clicked OUTSIDE the component.') } } render() { return( <span ref={node => this.node = node}> Level 0<br/> <span> Level 1.<br/> <span>Level 2.</span> </span> </span> ); } } ReactDOM.render(<Dialog/>, document.getElementById('View')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="View"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户在本地存储中单击的按钮的 ID 存储在 React 组件中? - How do I store the id of a button a user clicked in local storage in a react component? 如何在反应中通过循环检查组件内部的当前状态? - How to check current state inside component by loop in react? 如何仅禁用映射组件的单击按钮 - How do I disable only the Clicked button for a mapped over component 我如何定位被点击的 React 组件 - How do i target React component which is clicked React - 如何检测是否单击了子组件 - React - How do I detect if a child component is clicked 使用laravel身份验证时,如何检查用户是否已在“反应”组件中“登录”? - How do I check if user is 'logged in' in a react component when using laravel authentication? 如何获取在组件内部单击的子元素? - How to get child element that was clicked inside of a component? 如何使用 PnPJS 检查当前用户是否属于 sharepoint 组? - How can I check if current user is part of a sharepoint group with PnPJS? 单击 React 组件时,如何隐藏该组件的所有其他实例? - When a react component is clicked, how do I hide all other instances of that component? 如何在我的 class 组件中检测在功能组件中单击了哪个输入值? - How do I detect in my class component which input value's clicked in functional component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM