Skip to content

Instantly share code, notes, and snippets.

@kitcat-dev
Created May 3, 2022 09:06
Show Gist options
  • Save kitcat-dev/380a7254824c5af5fadfc986ec0dda22 to your computer and use it in GitHub Desktop.
Save kitcat-dev/380a7254824c5af5fadfc986ec0dda22 to your computer and use it in GitHub Desktop.
export const useToggle = (defaultValue = false) => {
const [isEnabled, set] = useReducer(
(state, next) => (next == null ? !state : next),
defaultValue
);
const enable = () => set(true);
const disable = () => set(false);
const toggle = () => set();
return { isEnabled, enable, disable, toggle };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment