aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-toggle/use-toggle.ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-10-30 11:18:11 +0100
committerArmand Philippot <git@armandphilippot.com>2023-11-11 18:15:27 +0100
commit84a679b0e48ed76eee2fa44d3caac83591aa3c8c (patch)
tree1d418a6c514ff8a04b84ba35c98736e8450f968c /src/utils/hooks/use-toggle/use-toggle.ts
parent60c49f18389ff625177a57277ef8f292a31097bf (diff)
feat(hooks): add useBoolean and useToggle hooks
Diffstat (limited to 'src/utils/hooks/use-toggle/use-toggle.ts')
-rw-r--r--src/utils/hooks/use-toggle/use-toggle.ts15
1 files changed, 15 insertions, 0 deletions
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;
+};