keycloak-scim/js/apps/admin-ui/src/utils/useToggle.ts

13 lines
468 B
TypeScript

import { useCallback, useState } from "react";
/**
* A hook that allows you toggle a boolean value, useful for toggle buttons, showing and hiding modals, etc.
*
* @param initialValue The initial value to use, false by default.
*/
export default function useToggle(initialValue = false) {
const [value, setValue] = useState(initialValue);
const toggleValue = useCallback(() => setValue((val) => !val), []);
return [value, toggleValue, setValue] as const;
}