diff --git a/libs/isograph-react-disposable-state/src/useDisposableState.ts b/libs/isograph-react-disposable-state/src/useDisposableState.ts index 81e18fffe..d5e979459 100644 --- a/libs/isograph-react-disposable-state/src/useDisposableState.ts +++ b/libs/isograph-react-disposable-state/src/useDisposableState.ts @@ -54,11 +54,11 @@ export function useDisposableState( return; } lastCommittedParentCache.current = parentCache; - // capture last set pair in a variable - const current = itemCleanupPairRef.current; + return () => { - // current is a stale variable - current?.[1](); + if (itemCleanupPairRef.current !== null) { + itemCleanupPairRef.current[1](); + } }; }, [parentCache], diff --git a/libs/isograph-react-disposable-state/src/useLazyDisposableState.ts b/libs/isograph-react-disposable-state/src/useLazyDisposableState.ts index 38da37c4c..42a857e65 100644 --- a/libs/isograph-react-disposable-state/src/useLazyDisposableState.ts +++ b/libs/isograph-react-disposable-state/src/useLazyDisposableState.ts @@ -36,11 +36,9 @@ export function useLazyDisposableState( return; } lastCommittedParentCache.current = parentCache; - // capture last set pair in a variable - const current = itemCleanupPairRef.current; + return () => { - // current is a stale variable - const cleanupFn = current?.[1]; + const cleanupFn = itemCleanupPairRef.current?.[1]; // TODO confirm useEffect is called in order. if (cleanupFn == null) { throw new Error(