UseBoolean

  1. import { useMemo, useState } from 'react';
  2. interface Actions {
  3. setTrue: () => void,
  4. setFalse: () => void,
  5. setToggle: () => void,
  6. }
  7. const useBoolean = (defaultValue: boolean = false): [boolean, Actions]=> {
  8. const [value, setValue] = useState<boolean>(defaultValue);
  9. const actions = useMemo(() => {
  10. const setTrue = () => setValue(true);
  11. const setFalse = () => setValue(false);
  12. const setToggle = () => setValue(!value);
  13. return { setTrue, setFalse, setToggle };
  14. }, [value]);
  15. return [
  16. value,
  17. actions
  18. ];
  19. };
  20. export default useBoolean;