UNPKG

@frauschert/yas

Version:

Yet Another State - A lightweight and type-safe state management solution for React

24 lines (23 loc) 1 kB
type Listener<T> = (state: T, previousState: T) => void; export type Middleware<T> = (store: Store<T>) => (next: (state: T) => void) => (state: T) => void; /** * Store interface */ export interface Store<T> { /** Returns the current state */ getState: () => T; /** Returns the initial state that was used to create the store */ getInitialState: () => T; /** Returns the previous state before the last update */ getPreviousState: () => T; /** Updates the state using the provided function */ setState: (fn: (state: T) => T) => void; /** * Subscribes to state changes * @param listener Function called when state changes with new and previous state * @returns Cleanup function to unsubscribe */ subscribe: (listener: Listener<T>) => () => void; } export declare function createStore<T extends Record<string, unknown>>(initialState: T, equalityFn?: (state: T, previousState: T) => boolean, ...middleware: Middleware<T>[]): Store<T>; export {};