@ehsaneha/react-observable-store
Version:
This is an extension of the `@ehsaneha/observable-store` library specifically tailored for React applications inspired by `zustand` library.
34 lines (33 loc) • 1.57 kB
TypeScript
import React from "react";
import { Actions, ObserverDeps, SetInputFunc, CreateActionsFunc, StoreCore as StoreCorePrimitive, Observer } from "@ehsaneha/observable-store";
import { Override } from "@ehsaneha/utils";
export type { Actions, CreateActionsFunc } from "@ehsaneha/observable-store";
export type StoreCore<S> = {
useState: UseState<S>;
useGet: UseGet<S>;
useOnChange: (func: Observer<S>, deps?: ObserverDeps<S>) => void;
} & StoreCorePrimitive<S>;
export type Store<S = undefined, TActions extends Actions = {}> = Override<StoreCore<S>, TActions>;
export type UseStateInputFunc<S, TReturn = void, TLocalState = TReturn extends void ? undefined : TReturn> = (storeState: S, localPrevState: TLocalState | undefined, storePrevState: S) => TReturn;
export type UseState<S> = {
(): [
S,
(newState: S | SetInputFunc<S>) => void,
React.Dispatch<React.SetStateAction<S>>
];
<TLocalState>(func?: UseStateInputFunc<S, TLocalState>, useEffectDeps?: React.DependencyList, deps?: ObserverDeps<S | undefined>): [
TLocalState,
(newState: S | SetInputFunc<S>) => void,
React.Dispatch<React.SetStateAction<TLocalState>>
];
};
export type UseGet<S> = {
(): S;
<TLocalState>(func?: UseStateInputFunc<S, TLocalState>): TLocalState;
};
export type CreateStore = {
(): Store<undefined>;
<S>(): Store<S | undefined>;
<S>(initState: S): Store<S>;
<S, TActions extends Actions>(initState: S, actions?: CreateActionsFunc<S, TActions>): Store<S, TActions>;
};