import React from "react";

export default function useBool(initial = false) {
  const [loading, setLoading] = React.useState(initial);
  const loadingRef = React.useRef();
  loadingRef.current = loading;

  const setTrue = React.useCallback(() => setLoading(true), []);
  const setFalse = React.useCallback(() => setLoading(false), []);
  const toggle = React.useCallback(() => setLoading(!loadingRef.current), []);

  return [loading, setTrue, setFalse, setLoading, toggle];
}
