Last active
June 10, 2019 18:57
-
-
Save gabemeola/4f39bdf895551cc7885413e65722433d to your computer and use it in GitHub Desktop.
Simple Toggle Hook for React
This file contains hidden or 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 { useReducer } from 'react'; | |
type Toggle = (forceToggle?: boolean) => void; | |
export default function useToggle(initial: boolean = false) { | |
return useReducer( | |
(toggledState: boolean, forceToggle?: boolean) => forceToggle || !toggledState, | |
initial | |
) as [boolean, Toggle]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment