Last active
June 29, 2020 12:38
-
-
Save finom/07d04733a996c20a4f3a0d3487fdbd28 to your computer and use it in GitHub Desktop.
[finom/use-boolean-state] Simplifies boolean useState creation
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
/* | |
Usage: | |
const [value, setTrue, setFalse] = useBooleanState(initialValue); | |
*/ | |
import { useState, useCallback } from "react"; | |
export default function useBooleanState(initialValue) { | |
const [value, onSetValue] = useState(initialValue); | |
const onSetTrue = useCallback(() => onSetValue(true), []); | |
const onSetFalse = useCallback(() => onSetValue(false), []); | |
return [value, onSetTrue, onSetFalse]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment