Musing about useBoolean
I saw a useBoolean utility hook the other day which looked something along the lines of:
const useBoolean = (defaultValue = false) => {
const [value, setValue] = useState(defaultValue);
const toggle = useCallback(() => {
setValue((value) => !value);
}, []);
const on = useCallback(() => {
setValue(true);
}, []);
const off = useCallback(() => {
setValue(false);
}, []);
return [
value,
{
toggle,
on,
off,
setValue,
},
];
};
It got me thinking about how simple terse I could make the implementation, so I decided to write things out to see what it might look like.
