Skip to content

Instantly share code, notes, and snippets.

@imgsrc
Created August 2, 2021 20:28
Show Gist options
  • Save imgsrc/0ae51840b6706f4a9c38a85aa7bb5e3f to your computer and use it in GitHub Desktop.
Save imgsrc/0ae51840b6706f4a9c38a85aa7bb5e3f to your computer and use it in GitHub Desktop.
change boolean state
export const useToggleBool = (defaultValue = false): [boolean, { open: VoidFunction; close: VoidFunction; toggle: VoidFunction }] => {
const [state, setState] = useState<boolean>(() => defaultValue);
const open = () => setState(true);
const close = () => setState(false);
const toggle = () => setState((prevState) => !prevState);
const methods = useMemo(() => ({ open, close, toggle }), []);
return [state, methods];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment