--- title: React useClickInside hook type: snippet tags: [hooks,effect,event] author: chalarangelo cover: digital-nomad-10 dateModified: 2020-11-16T14:17:53+02:00 --- Handles the event of clicking inside the wrapped component. - Create a custom hook that takes a `ref` and a `callback` to handle the `'click'` event. - Use the `useEffect()` hook to append and clean up the `click` event. - Use the `useRef()` hook to create a `ref` for your click component and pass it to the `useClickInside` hook. ```jsx const useClickInside = (ref, callback) => { const handleClick = e => { if (ref.current && ref.current.contains(e.target)) { callback(); } }; React.useEffect(() => { document.addEventListener('click', handleClick); return () => { document.removeEventListener('click', handleClick); }; }); }; ``` ```jsx const ClickBox = ({ onClickInside }) => { const clickRef = React.useRef(); useClickInside(clickRef, onClickInside); return (

Click inside this element

); }; ReactDOM.createRoot(document.getElementById('root')).render( alert('click inside')} /> ); ```