UNPKG

web3.db-fileconnector

Version:

GraphQL System Built on web3 technologies. Web3.DB is an open database built on top of web3 technologies. It is a decentralized, open-source database that allows users to store and query data in a secure and efficient manner. The system is designed to be

33 lines (31 loc) 1.21 kB
import React, { useState, useEffect, useContext, useRef } from "react"; export default function useOutsideClick(ref, handler) { useEffect( () => { const listener = (event) => { // Do nothing if clicking ref's element or descendent elements if (!ref.current || ref.current.contains(event.target)) { return; } handler(event); }; if (typeof window !== "undefined") { document.addEventListener("mousedown", listener); document.addEventListener("touchstart", listener); } return () => { if (typeof window !== "undefined") { document.removeEventListener("mousedown", listener); document.removeEventListener("touchstart", listener); } }; }, // Add ref and handler to effect dependencies // It's worth noting that because passed in handler is a new ... // ... function on every render that will cause this effect ... // ... callback/cleanup to run every render. It's not a big deal ... // ... but to optimize you can wrap handler in useCallback before ... // ... passing it into this hook. [ref, handler] ); }