UNPKG

usestore-react

Version:

A simple state management library for React using hooks

47 lines (46 loc) 1.86 kB
import { SetStateAction } from 'react'; declare type SetState<TState> = (state: SetStateAction<TState>) => void; export declare type Store<TState> = { /** * Unique name of the store */ readonly name: string; /** * Default state value */ readonly defaultState: TState; /** * Get the current value of the state */ readonly getState: () => TState; /** * Set the state of the store */ readonly setState: SetState<TState>; /** * useStore that is scoped to this specific store */ readonly useStore: () => [TState, SetState<TState>]; /** * useSelector that is scoped to this specific store */ readonly useSelector: <TValue>(selector: (state: TState) => TValue, equalityFunction?: (value: TValue, newValue: TValue) => boolean) => TValue; /** * Resets the store to its defaultState */ readonly reset: () => void; }; export declare type InternalStore<TState> = Store<TState> & { state: TState; setters: SetState<TState>[]; subscribe: (listener: (state: TState) => void) => void; unsubscribe: (listener: (state: TState) => void) => void; }; export declare const createStore: <TState>(name: string, defaultState: TState) => [() => TState, SetState<TState>, () => [TState, SetState<TState>]] & Store<TState>; export declare const getStore: <TState>(name: string) => InternalStore<TState>; export declare const hasStore: (name: string) => boolean; export declare const deleteStore: (name: string) => void; export declare const deleteAllStores: () => void; export declare const useStore: <TState>(name: string) => [TState, SetState<TState>]; export declare const useSelector: <TState, TValue>(name: string, selectorFn: (state: TState) => TValue, equalityFunction?: (value: TValue, newValue: TValue) => boolean) => TValue; export {};