import { Dispatch , SetStateAction , useCallback , useMemo , useState , } from "react" ; import { useCoralContext } from "coral-framework/lib/bootstrap" ; function useInMemoryState < S > ( key , initialState ) { const { inMemoryStorage } = useCoralContext ( ) ; const keyMemorized = useMemo ( ( ) => key , [ ] ) ; const value = inMemoryStorage . getItem ( keyMemorized ) ; const initState = value !== null ? value : initialState ; const [ state , setState ] = useState ( initState ) ; const setStateWrapped = useCallback ( ( s ) => { setState ( s ) ; inMemoryStorage . setItem ( keyMemorized , s ) ; } , [ inMemoryStorage , keyMemorized ] ) ; return [ state , setStateWrapped ] ; } export default useInMemoryState ;