rooks
Version:
Collection of awesome react hooks
39 lines (38 loc) • 1.2 kB
TypeScript
import type { Dispatch } from "react";
/**
* Use toggle hook helps you easily toggle a value
*
* @example
* const [value, toggle] = useToggle(); // initialValue defaults to false
* // value === false
* toggle();
* // value === true
*/
export declare function useToggle(): [boolean, () => void];
/**
* Use toggle hook helps you easily toggle a value
*
* @param initialValue the initial value of the boolean
* @example
* const [value, toggle] = useToggle(true);
* // value === true
* toggle();
* // value === false
*/
export declare function useToggle<S>(initialValue: S): [S, () => void];
/**
* Use toggle hook helps you easily toggle a value
*
* @param initialValue the initial value
* @param toggleFunction A toggle function. This allows for non boolean toggles and custom actions
* @example
* const [value, dispatch] = useToggle(1, (state, action) => {
* switch (action.type) {
* case "increment": return state + 1;
* case "decrement": return state - 1;
* default: return state;
* }
* });
* dispatch({ type: "increment" });
*/
export declare function useToggle<S, A>(initialValue: S, toggleFunction: (currentValue: S, action: A) => S): [S, Dispatch<A>];