Last active
December 30, 2021 20:04
-
-
Save codemile/321dcc7918b36c1fcf409eaf7f64843e to your computer and use it in GitHub Desktop.
Hook that toggles a boolean state.
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 {useCallback, useState} from 'react'; | |
const useToggle = (init: boolean = false): [boolean, (v: unknown) => void] => { | |
const [toggle, setToggle] = useState(init); | |
const callback = useCallback((value) => { | |
setToggle((prev) => (typeof value === 'boolean' ? value : !prev)); | |
}, []); | |
return [toggle, callback]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment