UNPKG

react-constore

Version:

Lightweight and efficient state management for React with TypeScript support

27 lines (24 loc) 1.09 kB
type State = Record<string, any>; type Listener<T = any> = (value: T, state: State) => void; type GlobalListener<T extends State> = (state: T) => void; type PartialState<T extends State> = Partial<T> | ((state: T) => Partial<T>); type SetStateAction<T> = T | ((prev: T) => T); interface StoreAPI<T extends State> { getState(): T; getState<K extends keyof T>(key: K): T[K]; setState(partial: PartialState<T>): void; subscribe(listener: GlobalListener<T>): () => void; subscribe(key: string, listener: Listener): () => void; useStore(): { state: T; setState: (partial: PartialState<T>) => void; changedKey: keyof T; }; useStoreKey<K extends keyof T>(key: K): [T[K], (value: SetStateAction<T[K]>) => void]; useStoreKeys<K extends keyof T>(keys: K[]): [ Pick<T, K>, (updates: Partial<Pick<T, K>> | ((values: Pick<T, K>) => Partial<Pick<T, K>>)) => void ]; } declare const createStore: <T extends State>(initialState: T | (() => T)) => StoreAPI<T>; export { type State, type StoreAPI, createStore as default };