1.9 KiB
1.9 KiB
ClickInside and ClickOutside
Two handy hooks to handle the click outside and inside event on the wrapped component.
- Create customized hooks that take in a
refcomponent(node) and acallbackfunction to hanlde the customizedclickevent - Use the
React.useEffect()hook to append and clean up theclickevent. - Use the
React.useRef()hook to create areffor your click component and pass it touseClickInsideanduseClickOutsidehooks.
.click-box {
border: 2px dashed orangered;
height: 200px;
width: 400px;
display: flex;
justify-content: center;
align-items: center;
}
p {
border: 2px solid blue;
padding: 16px;
}
const useClickInside = (ref, callback) => {
const handleClick = e => {
//use the node contains to verify if we click inside
if (ref.current && ref.current.contains(e.target)) {
callback();
}
};
//clean up using useEffect
useEffect(() => {
document.addEventListener("click", handleClick);
return () => {
document.removeEventListener("click", handleClick);
};
});
};
const useClickOutside = (ref, callback) => {
const handleClick = e => {
//use the node contains to verify if we click outside
if (ref.current && !ref.current.contains(e.target)) {
callback();
}
};
// clean up using useEffect
useEffect(() => {
document.addEventListener("click", handleClick);
return () => {
document.removeEventListener("click", handleClick);
};
});
};
function ClickBox({onClickOutside,onClickInside}) {
const clickRef = useRef();
useClickOutside(clickRef, onClickOutside);
useClickInside(clickRef, onClickInside);
return (
<div className="click-box" ref={clickRef}>
<p>Hello Click Me Inside!</p>
</div>
);
}
ReactDOM.render(<ClickBox onClickOutside={()=> alert("click outside")} onClickInside={()=> alert("click inside")}/>,document.getElementById('root'))