aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-toggle/use-toggle.ts
blob: c07c1e2ed52cd5e058b9de0f580b5f06a9bce119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
};