Skip to content

Instantly share code, notes, and snippets.

@finom
Last active June 29, 2020 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finom/07d04733a996c20a4f3a0d3487fdbd28 to your computer and use it in GitHub Desktop.
Save finom/07d04733a996c20a4f3a0d3487fdbd28 to your computer and use it in GitHub Desktop.
[finom/use-boolean-state] Simplifies boolean useState creation
/*
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