Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit b3f1db4

Browse files
chore(useAutoClosableDropdown): remove duplicated state in ref (#3031)
Co-authored-by: Ben Halverson <7907232+benhalverson@users.noreply.github.com>
1 parent be90ad7 commit b3f1db4

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/hooks/useAutoClosableDropdown.tsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React, { useEffect, useCallback } from 'react';
22
import { CommonComponents } from '../components';
33
import { DropdownItem } from '../types';
44

@@ -8,27 +8,22 @@ export const useAutoClosableDropdown = <T extends HTMLElement>(
88
) => {
99
const [shouldShow, setShouldShow] = React.useState(false);
1010

11-
// this is used as an event listener doesn't have access to the state
12-
const shouldShowRef = useRef(shouldShow);
13-
14-
const updateVisibility = (value: boolean) => {
11+
const updateVisibility = useCallback((value: boolean) => {
1512
setShouldShow(value);
16-
shouldShowRef.current = value;
17-
};
13+
}, []);
1814

19-
const hideDropdownIfVisible = () => {
20-
if (shouldShowRef.current === true) {
15+
const hideDropdownIfVisible = useCallback(() => {
16+
if (shouldShow) {
2117
updateVisibility(false);
2218
}
23-
};
19+
}, [updateVisibility, shouldShow]);
2420

2521
useEffect(() => {
2622
document.addEventListener('click', hideDropdownIfVisible, true);
2723

2824
return () =>
2925
document.removeEventListener('click', hideDropdownIfVisible, true);
30-
// eslint-disable-next-line react-hooks/exhaustive-deps
31-
}, []);
26+
}, [hideDropdownIfVisible]);
3227

3328
return {
3429
renderDropdown: (

0 commit comments

Comments
 (0)