Skip to content

Instantly share code, notes, and snippets.

@develohpanda
Last active July 8, 2020 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save develohpanda/e3f655df71bc7e7a8ce1b8616ebe9504 to your computer and use it in GitHub Desktop.
Save develohpanda/e3f655df71bc7e7a8ce1b8616ebe9504 to your computer and use it in GitHub Desktop.
useToggleState makes it cleaner to use a boolean state toggle
import * as React from 'react';
const useToggleState = (initialState: boolean = false): [boolean, () => void] => {
const [state, set] = React.useState(initialState);
const toggle = React.useCallback(() => set(oldState => !oldState), []);
return [state, toggle];
};
// Consume as
const [visible, toggleVisibility] = useToggleState(false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment