UNPKG

react-micro-reducer

Version:

A React reducer hook, with a "micro"-reducer style, made for a Typescript world 💙

15 lines (14 loc) • 1.12 kB
declare type TMicroActions<ReturnType> = { [key: string]: (...args: any[]) => ReturnType; }; declare type TMicroDispatch<R extends TMicroActions<any>> = { [Key in keyof R]: (...x: Parameters<R[Key]>) => void; }; declare type TMicroReducer<State, ReturnType = State> = (state: State) => TMicroActions<ReturnType>; export declare type MicroReducerReturn<R extends TMicroReducer<any, any>> = [MicroReducerState<R>, MicroReducerDispatch<R>]; export declare type MicroReducerState<R extends TMicroReducer<any, any>> = Parameters<R>[0]; export declare type MicroReducerDispatch<R extends TMicroReducer<any, any>> = TMicroDispatch<ReturnType<R>>; export declare type ProduceFunc = <State>(currentState: State, producer: (draftState: State) => void) => State; export declare function useMicroReducer<State, R extends TMicroActions<State>>(microReducer: (state: State) => R, initialState?: State): [State, TMicroDispatch<R>]; export declare function useMicroReducer<State, R extends TMicroActions<void>>(microReducer: (state: State) => R, initialState: State, producer: ProduceFunc): [State, TMicroDispatch<R>]; export {};