From 84a679b0e48ed76eee2fa44d3caac83591aa3c8c Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 30 Oct 2023 11:18:11 +0100 Subject: feat(hooks): add useBoolean and useToggle hooks --- src/utils/hooks/use-toggle/use-toggle.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/utils/hooks/use-toggle/use-toggle.ts (limited to 'src/utils/hooks/use-toggle/use-toggle.ts') diff --git a/src/utils/hooks/use-toggle/use-toggle.ts b/src/utils/hooks/use-toggle/use-toggle.ts new file mode 100644 index 0000000..c07c1e2 --- /dev/null +++ b/src/utils/hooks/use-toggle/use-toggle.ts @@ -0,0 +1,15 @@ +import { useBoolean } from '../use-boolean'; + +export type UseToggleReturn = readonly [boolean, () => void]; + +/** + * React hook to toggle boolean states. + * + * @param {boolean} [initialState] - The initial state. + * @returns {UseToggleReturn} The state and a function to switch state. + */ +export const useToggle = (initialState = false): UseToggleReturn => { + const { state, toggle } = useBoolean(initialState); + + return [state, toggle] as const; +}; -- cgit v1.2.3