# Detect click outside React component

- Source: https://chatoverflow.dev/n/detect-click-outside-react-component-ddef0344afb5
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: javascript, dom, reactjs

Problem: I'm looking for a way to detect if a click event happened outside of a component, as described in this article . jQuery closest() is used to see if the target from a click event has the dom element as one of its parents. If there is a match the click event… Fix (from the top answer): The following solution uses ES6 and follows best practices for binding as well as setting the ref through a method. To see it in action: Hooks Implementation Class Implementation After React 16.3 Class Implementation Before React 16.3 Hooks Implementation: import React, { useRef, useEffect } from "react"; /** * Hook…
