Skip to content

Commit be40fda

Browse files
authored
Fix re-rendering in strict mode(#523)
1 parent e334345 commit be40fda

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/hooks/useLoader.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {act, renderHook, waitFor} from '@testing-library/react';
2+
import {StrictMode} from 'react';
23
import {useLoader} from './useLoader';
34

45
describe('useLoader', () => {
@@ -404,4 +405,34 @@ describe('useLoader', () => {
404405

405406
await waitFor(() => expect(secondTime.current).toBe('foo'));
406407
});
408+
409+
it('should update the content in StrictMode', async () => {
410+
jest.useFakeTimers();
411+
412+
const delay = 10;
413+
const loader = jest.fn(
414+
() => new Promise(resolve => {
415+
setTimeout(() => resolve('foo'), delay);
416+
}),
417+
);
418+
419+
const {result} = renderHook(
420+
() => useLoader({
421+
cacheKey: cacheKey.current(),
422+
loader: loader,
423+
initial: 'bar',
424+
}),
425+
{
426+
wrapper: StrictMode,
427+
},
428+
);
429+
430+
// Let the loader resolve
431+
await act(async () => {
432+
jest.advanceTimersByTime(delay);
433+
await flushPromises();
434+
});
435+
436+
await waitFor(() => expect(result.current).toBe('foo'));
437+
});
407438
});

src/hooks/useLoader.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export function useLoader<R>({initial, ...currentOptions}: CacheOptions<R>): R {
3535

3636
useEffect(
3737
() => {
38+
mountedRef.current = true;
39+
3840
if (initial !== undefined) {
3941
load(currentOptions);
4042
}

0 commit comments

Comments
 (0)