gpt4 book ai didi

javascript - 检测 React 组件外部的点击

转载 作者:IT老高 更新时间:2023-10-28 13:11:32 27 4
gpt4 key购买 nike

我正在寻找一种方法来检测点击事件是否发生在组件之外,如 article 中所述。 . jQuery最接近()用于查看来自单击事件的目标是否将dom元素作为其父元素之一。如果存在匹配项,则单击事件属于其中一个子项,因此不被视为在组件之外。

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

click 事件包含像“path”这样的属性,它似乎保存了事件经过的 dom 路径。我不确定要比较什么或如何最好地遍历它,我想有人一定已经把它放在一个聪明的实用函数中......不是吗?

最佳答案

以下解决方案使用 ES6 并遵循最佳实践来绑定(bind)以及通过方法设置 ref。

查看实际效果:

钩子(Hook)实现:

import React, { useRef, useEffect } from "react";

/**
* Hook that alerts clicks outside of the passed ref
*/
function useOutsideAlerter(ref) {
useEffect(() => {
/**
* Alert if clicked on outside of element
*/
function handleClickOutside(event) {
if (ref.current && !ref.current.contains(event.target)) {
alert("You clicked outside of me!");
}
}
// Bind the event listener
document.addEventListener("mousedown", handleClickOutside);
return () => {
// Unbind the event listener on clean up
document.removeEventListener("mousedown", handleClickOutside);
};
}, [ref]);
}

/**
* Component that alerts if you click outside of it
*/
export default function OutsideAlerter(props) {
const wrapperRef = useRef(null);
useOutsideAlerter(wrapperRef);

return <div ref={wrapperRef}>{props.children}</div>;
}

类实现:

16.3 之后

import React, { Component } from "react";

/**
* Component that alerts if you click outside of it
*/
export default class OutsideAlerter extends Component {
constructor(props) {
super(props);

this.wrapperRef = React.createRef();
this.handleClickOutside = this.handleClickOutside.bind(this);
}

componentDidMount() {
document.addEventListener("mousedown", this.handleClickOutside);
}

componentWillUnmount() {
document.removeEventListener("mousedown", this.handleClickOutside);
}

/**
* Alert if clicked on outside of element
*/
handleClickOutside(event) {
if (this.wrapperRef && !this.wrapperRef.current.contains(event.target)) {
alert("You clicked outside of me!");
}
}

render() {
return <div ref={this.wrapperRef}>{this.props.children}</div>;
}
}

16.3 之前

import React, { Component } from "react";

/**
* Component that alerts if you click outside of it
*/
export default class OutsideAlerter extends Component {
constructor(props) {
super(props);

this.setWrapperRef = this.setWrapperRef.bind(this);
this.handleClickOutside = this.handleClickOutside.bind(this);
}

componentDidMount() {
document.addEventListener("mousedown", this.handleClickOutside);
}

componentWillUnmount() {
document.removeEventListener("mousedown", this.handleClickOutside);
}

/**
* Set the wrapper ref
*/
setWrapperRef(node) {
this.wrapperRef = node;
}

/**
* Alert if clicked on outside of element
*/
handleClickOutside(event) {
if (this.wrapperRef && !this.wrapperRef.contains(event.target)) {
alert("You clicked outside of me!");
}
}

render() {
return <div ref={this.setWrapperRef}>{this.props.children}</div>;
}
}

关于javascript - 检测 React 组件外部的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32553158/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com