Skip to content

Commit 6ee97ec

Browse files
authored
Merge pull request #1771 from crimx/patch-3
fix useUnmountPromise stops when component is updated
2 parents fa9d123 + e3adac0 commit 6ee97ec

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/useUnmountPromise.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { useEffect, useMemo, useRef } from 'react';
1+
import { useMemo, useRef } from 'react';
2+
import useEffectOnce from './useEffectOnce';
23

34
export type Race = <P extends Promise<any>, E = any>(promise: P, onError?: (error: E) => void) => P;
45

56
const useUnmountPromise = (): Race => {
67
const refUnmounted = useRef(false);
7-
useEffect(() => () => {
8+
useEffectOnce(() => () => {
89
refUnmounted.current = true;
910
});
1011

tests/useUnmountPromise.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ describe('useUnmountPromise', () => {
3535
]);
3636
expect(res).toBe('UNMOUNTED');
3737
});
38+
39+
it('should resolve promise when component is updated', async () => {
40+
const hook = renderHook(() => useUnmountPromise());
41+
42+
const mounted = hook.result.current;
43+
const pRes = mounted(new Promise(r => setTimeout(() => r(25), 10)));
44+
45+
hook.rerender();
46+
47+
const res = await pRes;
48+
49+
expect(res).toBe(25);
50+
});
3851

3952
it('when component is mounted function should resolve with wrapped promises - 2', async () => {
4053
const hook = renderHook(() => useUnmountPromise());

0 commit comments

Comments
 (0)