Created
June 21, 2022 20:57
-
-
Save alexcarpenter/062ea7740c012fde49cb032ce54d269f to your computer and use it in GitHub Desktop.
useBoolean hook
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
function useBoolean(initialState = false) { | |
const [state, setState] = React.useState(initialState) | |
const handlers = React.useMemo( | |
() => ({ | |
on: () => { | |
setState(true) | |
}, | |
off: () => { | |
setState(false) | |
}, | |
toggle: () => { | |
setState(s => !s) | |
}, | |
reset: () => { | |
setState(initialState) | |
}, | |
}), | |
[initialState] | |
) | |
return [state, handlers] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment