Skip to content

Commit f42c879

Browse files
committed
Cleanup the mess
1 parent f10f7cb commit f42c879

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/useClickOutside.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef } from 'react';
1+
import { useCallback, useRef } from 'react';
22
import { useOnClick } from 'useOnClick';
33

44
export type ClickOutsideOptions = {
@@ -24,12 +24,15 @@ const checkOptions = (event: MouseEvent, options?: ClickOutsideOptions) => {
2424
export const useOnClickOutside = (fn: () => void, options?: ClickOutsideOptions) => {
2525
const elementRef = useRef<HTMLElement>(null);
2626

27-
const handleClick = (event: MouseEvent) => {
28-
if (!elementRef?.current?.contains(event.target as Node) && checkOptions(event, options)) {
29-
// clicked outside the ref
30-
fn();
31-
}
32-
};
27+
const handleClick = useCallback(
28+
(event: MouseEvent) => {
29+
if (!elementRef?.current?.contains(event.target as Node) && checkOptions(event, options)) {
30+
// clicked outside the ref
31+
fn();
32+
}
33+
},
34+
[fn, options],
35+
);
3336

3437
useOnClick(handleClick);
3538

0 commit comments

Comments
 (0)