diff --git a/packages/store/src/core/hooks.ts b/packages/store/src/core/hooks.ts index 53ff12c..3cff2b6 100644 --- a/packages/store/src/core/hooks.ts +++ b/packages/store/src/core/hooks.ts @@ -53,15 +53,24 @@ export function useStaticStore( export function useStaticStore( item?: string ): [StateType, SetStoreFn] { - return [ - Manager.get(item), - function (state: SetStateAction) { - const newState: any = Manager.get(); - newState[item as any] = state; - - Manager.set(newState); - }, - ]; + const stateSetter: SetStoreFn = (value) => { + let stateValue: State = value as State; + let stateToSet = stateValue; + + if (typeof value === 'function') { + const callableState = value as (prevState: StateType) => State; + stateValue = callableState(Manager.get(item)); + } + + if (item) { + stateToSet = { [item]: stateValue }; + } + + Manager.set(stateToSet); + Manager.applyPluginHook('onSave', Manager.get()); + }; + + return [Manager.get(item), stateSetter]; } export function useReducer( @@ -75,3 +84,7 @@ export function setStore( ) { return Manager.set(item); } + +export function getStore(item?: string): StateType { + return Manager.get(item); +} diff --git a/packages/store/src/index.ts b/packages/store/src/index.ts index 29db90c..69301d6 100644 --- a/packages/store/src/index.ts +++ b/packages/store/src/index.ts @@ -1,4 +1,11 @@ export * from './@types'; export * from './plugins/'; -export { createStore, useStore, setStore, useReducer } from './core/hooks'; +export { + createStore, + useStore, + useStaticStore, + setStore, + getStore, + useReducer, +} from './core/hooks';