useBoolean

A custom hook optimized for turning things on and off.

import React, { useState, useCallback, useMemo } from "react";
function useBoolean(initialState) {
const [state, setState] = useState(false);
const setTrue = useCallback(() => setState(true), []);
const setFalse = useCallback(() => setState(false), []);
const toggle = useCallback(() => setState((state) => !state), []);
const actions = useMemo(() => ({ setState, toggle, setTrue, setFalse }), [
setFalse,
setTrue,
toggle,
]);
return useMemo(() => [state, actions], [actions, state]);
}