UNPKG

@domeadev/react-store-context

Version:

A simple and lightweight store context for React.

76 lines (68 loc) 3.41 kB
import * as zustand_vanilla from 'zustand/vanilla'; import * as zustand from 'zustand'; import { StoreApi, StateCreator } from 'zustand'; export * from 'zustand'; import * as entity_state_adapter from 'entity-state-adapter'; import { IdSelector, Comparer, Predicate, Update, EntityMap } from 'entity-state-adapter'; import * as react_jsx_runtime from 'react/jsx-runtime'; import * as react from 'react'; import { PropsWithChildren, ReactNode } from 'react'; export * from 'zustand/shallow'; export { combine } from 'zustand/middleware'; export * from 'zustand/traditional'; type AnyObject = Record<string, any>; type WithOptionalStore<Props, State> = Props & { store?: StoreApi<State>; }; type InferStoreState<T> = T extends StoreApi<infer S> ? S : never; /** * The default store creator function. */ declare function createDefaultStore<S extends AnyObject, A extends AnyObject>(initialState: S, actionsCreator: StateCreator<S, [], [], A>): zustand_vanilla.StoreApi<Omit<S, keyof A> & A>; declare function createEntityStateStore<T>(options?: { entities?: T[]; selectId?: IdSelector<T>; sortComparer?: false | Comparer<T>; }): zustand.StoreApi<Omit<entity_state_adapter.EntityState<T>, "map" | "addOne" | "addMany" | "addAll" | "removeOne" | "removeMany" | "removeAll" | "updateOne" | "updateMany" | "upsertOne" | "upsertMany" | "selectIds" | "selectEntities" | "selectAll" | "selectTotal" | "selectOne"> & { addOne: (entity: T) => void; addMany: (entities: T[]) => void; addAll: (entities: T[]) => void; removeOne: { (key: string): void; (key: number): void; }; removeMany: { (keys: string[]): void; (keys: number[]): void; (predicate: Predicate<T>): void; }; removeAll: () => void; updateOne: (update: Update<T>) => void; updateMany: (updates: Update<T>[]) => void; upsertOne: (entity: T) => void; upsertMany: (entities: T[]) => void; map: (mapFn: EntityMap<T>) => void; selectIds: () => string[] | number[]; selectEntities: () => entity_state_adapter.Dictionary<T>; selectAll: () => T[]; selectTotal: () => number; selectOne: (id: string | number) => T | undefined; }>; declare function createStoreContext<S, P extends AnyObject = AnyObject>(storeCreator: (props?: P) => StoreApi<S>): { StoreContext: react.Context<StoreApi<S> | null>; StoreProvider: (props: PropsWithChildren<WithOptionalStore<P, S>>) => react_jsx_runtime.JSX.Element; useCreateStore: (props?: P | (() => P)) => StoreApi<S>; useStore: <T>(selector: (state: S) => T, store?: StoreApi<S>) => T; createStoreSelector: <T>(selector: (state: S) => T) => (state: S) => T; Subscribe: <T>({ selector, children, store, }: { selector: (state: S) => T; children: (value: T) => ReactNode; store?: StoreApi<S>; }) => react_jsx_runtime.JSX.Element; }; declare function extend<Store extends StoreApi<any>, S extends AnyObject = AnyObject, A extends AnyObject = AnyObject>(store: Store, extension: { state: S; actions?: StateCreator<S, [], [], A>; }): StoreApi<InferStoreState<Store> & S & A>; declare function select<S extends StoreApi<any>, T>(store: S, selector: (state: InferStoreState<S>) => T): T; export { type AnyObject, type InferStoreState, type WithOptionalStore, createDefaultStore, createEntityStateStore, createStoreContext, extend, select };