Skip to content

Instantly share code, notes, and snippets.

@alexcarpenter
Created June 21, 2022 20:57
Show Gist options
  • Save alexcarpenter/062ea7740c012fde49cb032ce54d269f to your computer and use it in GitHub Desktop.
Save alexcarpenter/062ea7740c012fde49cb032ce54d269f to your computer and use it in GitHub Desktop.
useBoolean hook
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