Understand the implementation of useClickOutside() custom hook in react and how it can be used to implement Modal like functionality.
Anuj Sharma
Last Updated Nov 16, 2025
Detecting click outside a specific element is one of the common use-cases in the react development, and this functionality can be simplified using a reusable useClickOutside custom hook in react.
In this blog, we will go through the useClickOutside hook implementation and its real-life use-case.
Let's quickly jump into the implementation of react useClickOutside hook that detects clicks outside a specified element.
import { useEffect } from 'react';
// Custom Hook
const useClickOutside = (ref, callback) => {
const handleClick = (e) => {
if (ref.current && !ref.current.contains(e.target)) {
callback();
}
};
useEffect(() => {
document.addEventListener('click', handleClick);
return () => {
document.removeEventListener('click', handleClick);
};
}, [ref, callback]);
};
export default useClickOutside;
The useClickOutside custom hook can be used to handle scenarios where you want to close a dropdown, modal, or any other component when the user clicks outside of it.
Let's consider a simple example where we have a modal component that should close when the user clicks outside of the modal.
import React, { useRef } from 'react';
import useClickOutside from './useClickOutside';
const Modal = ({ onClose }) => {
const modalRef = useRef(null);
// Close the modal when clicking outside of it
useClickOutside(modalRef, onClose);
return (
<div style={styles.overlay}>
<div style={styles.modal} ref={modalRef}>
<h2>Example Modal</h2>
<p>This modal closes when you click outside of it.</p>
<button onClick={onClose}>Close</button>
</div>
</div>
);
};
const styles = {
overlay: {
position: 'fixed',
top: 0,
left: 0,
width: '100vw',
height: '100vh',
backgroundColor: 'rgba(0,0,0,0.5)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
modal: {
background: '#fff',
padding: '20px',
borderRadius: '8px',
width: '300px',
boxShadow: '0 0 10px rgba(0,0,0,0.3)',
},
};
export default Modal;
import React, { useState } from 'react';
import Modal from './Modal';
const App = () => {
const [open, setOpen] = useState(false);
return (
<div>
<button onClick={() => setOpen(true)}>Open Modal</button>
{open && <Modal onClose={() => setOpen(false)} />}
</div>
);
};
export default App;
In this example, the Modal component will automatically close when the user clicks outside of it because of the usage of useClickOutside custom hook.
Not we have covered the implementation of useClickOutside custom hook in React to handle click events outside a specified element when working with components like modals, drop-downs, or popovers.
Understanding useClickOutside hook implementation will help you in react interview since this can be used in the implementation of multiple react component implementation questions.
Happy coding!
Be the first to share your thoughts!
No comments yet.
Start the conversation!
Build Your Portfolio
Help the Community
Strengthen Your Skills
Share your knowledge by writing a blog or quick notes. Your contribution can help thousands of frontend developers ace their interviews and grow their careers! 🚀
Anuj Sharma
Last Updated Nov 10, 2025
Find the top React Performance Optimization Techniques specific to React applications that help to make your react app faster and more responsive for the users along with some bonus techniques.
Anuj Sharma
Last Updated Nov 10, 2025
Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Nov 16, 2025
Implement useThrottle Custom Hook In React (Interview) to limit the number of APi calls to improve the performance of application.
Anuj Sharma
Last Updated Oct 2, 2025
Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.
Anuj Sharma
Last Updated Nov 11, 2025
Understand step by step how to flatten nested array in javascript using recursion, also explore the flatten of complex array of object.
Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2025 FrontendGeek. All rights reserved