UNPKG

use-silly-hooks

Version:

A collection of custom React hooks to simplify state management in your applications. 🚀

125 lines (118 loc) • 6.11 kB
import { Dispatch, SetStateAction } from 'react'; /** * A custom hook that provides a boolean state with utility functions to manipulate it. * * @param {boolean} [initialValue=false] - The initial value of the boolean state (default `false`). * @returns {[ * value: boolean, * setValue: Dispatch<SetStateAction<boolean>>, * setTrue: () => void, * setFalse: () => void, * toggle: () => void, * reset: () => void * ]} * An array/tuple containing: * - `value`: The current boolean state. * - `setValue`: A function to set the state. * - `setTrue`: A function to set the state to true. * - `setFalse`: A function to set the state to false. * - `toggle`: A function to toggle the state. * - `reset`: A function to reset the state to the initial value. */ declare const useBoolean: (initialValue?: boolean) => [value: boolean, setValue: Dispatch<SetStateAction<boolean>>, setTrue: () => void, setFalse: () => void, toggle: () => void, reset: () => void]; /** * A custom hook that provides a string state with utility functions to manipulate it. * * @param {string} [initialValue=''] - The initial value of the string state (default `''`). * @returns {[ * value: string, * setValue: React.Dispatch<React.SetStateAction<string>>, * transform: (type: StringTransformType) => void, * reset: () => void * ]} * An array/tuple containing: * - `value`: The current string state. * - `setValue`: A function to set the state. * - `transform`: A function to transform the string state based on the specified type. * - `reset`: A function to reset the state to the initial value. */ type StringTransformType = 'lowercase' | 'uppercase' | 'capitalize' | 'pascal-case' | 'camel-case' | 'kebab-case' | 'snake-case'; declare const useString: (initialValue?: string) => [value: string, setValue: React.Dispatch<React.SetStateAction<string>>, transform: (type: StringTransformType) => void, reset: () => void]; /** * A custom hook that provides a number state with utility functions to manipulate it. * * @param {number} [initialValue=0] - The initial value of the number state (default `0`). * @returns {[ * value: number, * setValue: React.Dispatch<React.SetStateAction<number>>, * increment: (step?: number) => void, * decrement: (step?: number) => void, * multiply: (factor: number) => void, * divide: (divisor: number) => void, * reset: () => void, * setMin: (min: number) => void * setMax: (max: number) => void * ]} * An array/tuple containing: * - `value`: The current number state. * - `setValue`: A function to set the state. * - `increment`: A function to increment the state by a given step. * - `decrement`: A function to decrement the state by a given step. * - `multiply`: A function to multiply the state by a given factor. * - `divide`: A function to divide the state by a given divisor. * - `reset`: A function to reset the state to the initial value. * - `setMin`: A function to set the state to the maximum of the current state and a given minimum value. * - `setMax`: A function to set the state to the minimum of the current state and a given maximum value. */ declare const useNumber: (initialValue?: number) => [value: number, setValue: React.Dispatch<React.SetStateAction<number>>, increment: (step?: number) => void, decrement: (step?: number) => void, multiply: (factor: number) => void, divide: (divisor: number) => void, reset: () => void, setMin: (min: number) => void, setMax: (max: number) => void]; /** * A custom hook that provides an array state with utility functions to manipulate it. * * @template T - The type of elements in the array. * @param {T[]} [initialValue=[]] - The initial value of the array state (default `[]`). * @returns {[ * value: T[], * setValue: React.Dispatch<React.SetStateAction<T[]>>, * push: (item: T) => void, * pop: () => void, * shift: () => void, * unshift: (item: T) => void, * clear: () => void, * reset: () => void * ]} * An array/tuple containing: * - `value`: The current array state. * - `setValue`: A function to set the state. * - `push`: A function to add an item to the end of the array. * - `pop`: A function to remove the last item from the array. * - `shift`: A function to remove the first item from the array. * - `unshift`: A function to add an item to the beginning of the array. * - `clear`: A function to clear the array. * - `reset`: A function to reset the array to the initial value. */ declare const useArray: <T>(initialValue?: T[]) => [value: T[], setValue: React.Dispatch<React.SetStateAction<T[]>>, push: (item: T) => void, pop: () => void, shift: () => void, unshift: (item: T) => void, clear: () => void, reset: () => void]; /** * A custom hook that provides an object state with utility functions to manipulate it. * * @template T - The type of the object. * @param {T} initialValue - The initial value of the object state. * @returns {[ * value: T, * setValue: React.Dispatch<React.SetStateAction<T>>, * setProperty: <K extends keyof T>(key: K, newValue: T[K]) => void, * removeProperty: <K extends keyof T>(key: K) => void, * clear: () => void, * reset: () => void, * merge: (newObject: Partial<T>) => void * ]} * An array/tuple containing: * - `value`: The current object state. * - `setValue`: A function to set the state. * - `setProperty`: A function to set a property of the object. * - `removeProperty`: A function to remove a property from the object. * - `clear`: A function to clear the object. * - `reset`: A function to reset the object to the initial value. * - `merge`: A function to merge a new object into the current state. */ declare const useObject: <T extends object>(initialValue: T) => [value: T, setValue: React.Dispatch<React.SetStateAction<T>>, setProperty: <K extends keyof T>(key: K, newValue: T[K]) => void, removeProperty: <K extends keyof T>(key: K) => void, clear: () => void, reset: () => void, merge: (newObject: Partial<T>) => void]; export { type StringTransformType, useArray, useBoolean, useNumber, useObject, useString };