import {useState} from 'react'; import {useEffectAfterFirstRender} from './useEffectAfterFirstRender'; import usePrevious from './usePrevious'; const useMemoWithPrevious = ( factory: (previousInstance: T | null) => T, deps: React.DependencyList ): T => { const [value, setValue] = useState(() => factory(null)); const previous = usePrevious(value); useEffectAfterFirstRender(() => { setValue(factory(previous)); }, deps); return value; }; export {useMemoWithPrevious};