From c8d6a768df36cef1dc4ffe167d720ace48c33b50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Wa=C5=82ach?= <35966385+PatrykWalach@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:42:13 +0100 Subject: [PATCH] add comment for guards added in `useEffects` --- .../src/useCachedResponsivePrecommitValue.ts | 4 ++++ .../src/useLazyDisposableState.ts | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/libs/isograph-react-disposable-state/src/useCachedResponsivePrecommitValue.ts b/libs/isograph-react-disposable-state/src/useCachedResponsivePrecommitValue.ts index ee5460a5..21c2c9c9 100644 --- a/libs/isograph-react-disposable-state/src/useCachedResponsivePrecommitValue.ts +++ b/libs/isograph-react-disposable-state/src/useCachedResponsivePrecommitValue.ts @@ -51,6 +51,10 @@ export function useCachedResponsivePrecommitValue( const lastCommittedParentCache = useRef | null>(null); useEffect(() => { + // react reruns all `useEffect` in HMR since it doesn't know if the + // code inside of useEffect has changed. Since this is a library + // user can't change this code so we are safe to skip this rerun. + // This also prevents `useEffect` from running twice in Strict Mode. if (lastCommittedParentCache.current === parentCache) { return; } diff --git a/libs/isograph-react-disposable-state/src/useLazyDisposableState.ts b/libs/isograph-react-disposable-state/src/useLazyDisposableState.ts index 42a857e6..930752c3 100644 --- a/libs/isograph-react-disposable-state/src/useLazyDisposableState.ts +++ b/libs/isograph-react-disposable-state/src/useLazyDisposableState.ts @@ -32,6 +32,10 @@ export function useLazyDisposableState( const lastCommittedParentCache = useRef | null>(null); useEffect(() => { + // react reruns all `useEffect` in HMR since it doesn't know if the + // code inside of useEffect has changed. Since this is a library + // user can't change this code so we are safe to skip this rerun. + // This also prevents `useEffect` from running twice in Strict Mode. if (lastCommittedParentCache.current === parentCache) { return; }