Detect click outside React component using hooks
问题 I am finding that I am reusing behaviour across an app that when a user clicks outside an element I can hide it. With the introduction of hooks is this something I could put in a hook and share across components to save me writing the same logic in every component? I have implemented it once in a component as follows. const Dropdown = () => { const [isDropdownVisible, setIsDropdownVisible] = useState(false); const wrapperRef = useRef<HTMLDivElement>(null); const handleHideDropdown = (event: