diff --git a/src/tools/useGuaranteedMemo.ts b/src/tools/useGuaranteedMemo.ts index 5926c68..4756ee6 100644 --- a/src/tools/useGuaranteedMemo.ts +++ b/src/tools/useGuaranteedMemo.ts @@ -1,4 +1,4 @@ -import { useRef } from "react"; +import { useState } from "react"; /** Like react's useMemo but with guarantee that the fn * won't be invoked again if deps hasn't change */ @@ -6,10 +6,12 @@ export function useGuaranteedMemo( fn: () => T, deps: React.DependencyList ): T { - const ref = useRef<{ v: T; prevDeps: unknown[] }>(); + const [ref] = useState<{ + current: { v: T; prevDeps: unknown[] } | undefined; + }>({ "current": undefined }); if ( - !ref.current || + ref.current === undefined || deps.length !== ref.current.prevDeps.length || ref.current.prevDeps.map((v, i) => v === deps[i]).indexOf(false) >= 0 ) {