Skip to content

Commit

Permalink
don't use stale itemCleanupPairRef
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrykWalach committed Jan 3, 2025
1 parent 5bc585f commit ef89d4d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ export function useDisposableState<T = never>(
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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ export function useLazyDisposableState<T>(
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(
Expand Down

0 comments on commit ef89d4d

Please sign in to comment.