-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathuseSwitcher.ts
More file actions
18 lines (14 loc) · 647 Bytes
/
useSwitcher.ts
File metadata and controls
18 lines (14 loc) · 647 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { useCallback, useState } from 'react';
/**
* @param defaultValue initial value of the switch. Default {@link false}
* @example
* const [isOpen, turnIsOpenOn, turnIsOpenOff, toggleIsOpen] = useSwitcher();
*/
const useSwitcher = (defaultValue: boolean = false): readonly [boolean, () => void, () => void, () => void] => {
const [state, setState] = useState(defaultValue);
const turnOn = useCallback(() => setState(true), []);
const turnOff = useCallback(() => setState(false), []);
const toggle = useCallback(() => setState((s) => !s), []);
return [state, turnOn, turnOff, toggle] as const;
};
export default useSwitcher;