Detect click outside React component
@lebyy▲ 0 net votes
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…