Skip to content

Commit d293fe0

Browse files
eunwoo-leviclaude
andcommitted
fix(core/hooks): call cleanup when unmount occurs before async effect resolves
The cleanup function returned from the async effect was silently lost if the component unmounted before the Promise resolved. Added an `isCleaned` flag so the cleanup is invoked immediately after the Promise settles in that case. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 2a901bb commit d293fe0

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

packages/core/src/hooks/useAsyncEffect/useAsyncEffect.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ import { DependencyList, useEffect } from 'react';
2424
export function useAsyncEffect(effect: () => Promise<void | (() => void)>, deps?: DependencyList) {
2525
useEffect(() => {
2626
let cleanup: (() => void) | void;
27+
let isCleaned = false;
2728

2829
effect().then(result => {
2930
cleanup = result;
31+
if (isCleaned) {
32+
cleanup?.();
33+
}
3034
});
3135

3236
return () => {
37+
isCleaned = true;
3338
cleanup?.();
3439
};
3540

0 commit comments

Comments
 (0)