UNPKG

@supunlakmal/hooks

Version:

A collection of reusable React hooks

48 lines (47 loc) 2.56 kB
import { type Dispatch } from 'react'; type StateInitializerFN<State> = () => State; type StateUpdaterFN<State, PreviousState = State> = (previousState: PreviousState) => State; export type InitialState<State> = State | StateInitializerFN<State>; export type NextState<State, PreviousState = State> = State | StateUpdaterFN<State, PreviousState>; /** * Like `useState`, but every value set is passed through a mediator function * before the state is updated. The initial state is NOT mediated by default. * @template State The type of the state managed by the hook (after mediation). * @template RawState The type of the value passed to the setter (before mediation). Defaults to State. * @param initialState Initial state value or initializer function. Resolved value is used directly. * @param mediator Optional function that takes the resolved raw value and returns the final state value. Applied only on updates. */ export declare const useMediatedState: <State, RawState = State>(initialState: InitialState<State>, mediator?: (resolvedRawValue: RawState) => State) => [State, Dispatch<NextState<RawState, State>>]; export type CounterActions = { /** * Returns the current value of the counter. (Getter removed, use state value directly) */ /** * Increment the counter by the given `delta`. Clamped by min/max. * @param delta Amount to increment by. Can be a number or function `(prevValue) => number`. Defaults to 1. */ inc: (delta?: NextState<number, number>) => void; /** * Decrement the counter by the given `delta`. Clamped by min/max. * @param delta Amount to decrement by. Can be a number or function `(prevValue) => number`. Defaults to 1. */ dec: (delta?: NextState<number, number>) => void; /** * Set the counter to any value. Clamped by min/max. * @param value New value or function `(prevValue) => number`. */ set: (value: NextState<number, number>) => void; /** * Resets the counter to its originally resolved and clamped initial value. */ reset: () => void; }; /** * Tracks a numeric value with optional min/max boundaries and provides counter actions. * * @param initialValue The initial value (or initializer function). Defaults to 0. * @param max Optional maximum value. Initial value is clamped if needed. * @param min Optional minimum value. Initial value is clamped if needed. */ export declare const useCounter: (initialValue?: InitialState<number>, max?: number, min?: number) => [number, CounterActions]; export {};