useOutsideClickRef
Aboutβ
A hook that can track a click event outside a ref. Returns a callbackRef.
Installationβ
npm install --save rooks
Importing the hookβ
import { useOutsideClickRef } from "rooks";
Usageβ
function Demo() {
function outsidePClick() {
alert("Clicked outside p");
}
const [ref] = useOutsideClickRef(outsidePClick);
return (
<div>
<p ref={ref}>Click outside me</p>
</div>
);
}
render(<Demo />);
Argumentsβ
Arguments | Type | Description | Default value |
---|---|---|---|
handler | function | Callback to fire on outside click | |
when | boolean | A boolean which which activates the hook only when it is true. Useful for conditionally enable the outside click | true |
Returnsβ
Returned Array items | Type | Description |
---|---|---|
ref | CallbackRef | ref |
Codesandbox Examplesβ
Basic usageβ
Join Bhargav's discord serverβ
You can click on the floating discord icon at the bottom right of the screen and talk to us in our server.