import { useHookedReducer } from "./useReducer" ; import { useMemo , useContext , useState as useReactState } from "react" ; import { EnhancedStore , StateInspectorContext } from "./context" ; type StateAction < S > = S | ( ( s : S ) => S ) ; function stateReducer < S > ( state , action ) { return typeof action === "function" ? ( action as ( s : S ) => S ) ( state ) : action ; } export const useState = < S > ( initialState , id ) => { const inspectorStore = useContext ( StateInspectorContext ) ; const [ store , reducerId ] = useMemo < [ EnhancedStore | undefined , string | number ] > ( ( ) => [ inspectorStore , id ] , [ ] ) ; if ( ! store || ! reducerId ) { return useReactState < S > ( initialState ) ; } const finalInitialState = useMemo < S > ( ( ) => typeof initialState === "function" ? ( initialState as ( ) => S ) ( ) : initialState , [ ] ) ; return useHookedReducer < S , any > ( stateReducer , finalInitialState , store , reducerId ) ; } ;