From 9143bd064c1178ef9363b1f3ff4a7eb34a3bb8c6 Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Thu, 21 Mar 2024 12:34:45 +0100 Subject: [PATCH] #209 --- src/tools/useGuaranteedMemo.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 ) {