mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Remove TimeoutToggle callback's dependency on initial value
This commit is contained in:
parent
7a7884f38d
commit
be1b474f41
1 changed files with 4 additions and 3 deletions
|
@ -15,16 +15,17 @@ export const useTimeoutToggle = (
|
||||||
clearTimeout = window.clearTimeout,
|
clearTimeout = window.clearTimeout,
|
||||||
): [boolean, () => void] => {
|
): [boolean, () => void] => {
|
||||||
const [flag, setFlag] = useState<boolean>(initialValue);
|
const [flag, setFlag] = useState<boolean>(initialValue);
|
||||||
|
const initialValueRef = useRef(initialValue);
|
||||||
const timeout = useRef<number | undefined>(undefined);
|
const timeout = useRef<number | undefined>(undefined);
|
||||||
const callback = useCallback(() => {
|
const callback = useCallback(() => {
|
||||||
setFlag(!initialValue);
|
setFlag(!initialValueRef.current);
|
||||||
|
|
||||||
if (timeout.current) {
|
if (timeout.current) {
|
||||||
clearTimeout(timeout.current);
|
clearTimeout(timeout.current);
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout.current = setTimeout(() => setFlag(initialValue), delay);
|
timeout.current = setTimeout(() => setFlag(initialValueRef.current), delay);
|
||||||
}, [clearTimeout, delay, initialValue, setTimeout]);
|
}, [clearTimeout, delay, setTimeout]);
|
||||||
|
|
||||||
return [flag, callback];
|
return [flag, callback];
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue