1.先获取当前要监听的那个组件的ref
import React, { useRef } from 'react';
// 类组件获取
this.Ref = React.createRef();
// 函数组件获取
const Ref = useRef();
2.通过addEventListener来全局监听
document.addEventListener("mousedown", (e) => {
if (Ref.contains(e.target)) {
console.log("点击组件里面");
} else {
console.log("点击组件外面");
}
});