Last active
July 8, 2020 00:57
-
-
Save develohpanda/e3f655df71bc7e7a8ce1b8616ebe9504 to your computer and use it in GitHub Desktop.
useToggleState makes it cleaner to use a boolean state toggle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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