From 0a861e7eedecfd07e90c8af2ff1eff14a7c4a83c Mon Sep 17 00:00:00 2001 From: "Junius Free B. Fontamillas" <8164667+juniusfree@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:23:23 +0800 Subject: [PATCH 1/2] fix: useQuery hook does not refetch after resetApiState --- .../toolkit/src/query/react/buildHooks.ts | 8 +-- .../src/query/tests/buildHooks.test.tsx | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/packages/toolkit/src/query/react/buildHooks.ts b/packages/toolkit/src/query/react/buildHooks.ts index daec42efe2..2e595df5ec 100644 --- a/packages/toolkit/src/query/react/buildHooks.ts +++ b/packages/toolkit/src/query/react/buildHooks.ts @@ -1035,8 +1035,6 @@ export function buildHooks({ skipPollingIfUnfocused, }) - const lastRenderHadSubscription = useRef(false) - // TODO: Change this to `useRef>(undefined)` after upgrading to React 19. /** * @todo Change this to `useRef>(undefined)` after upgrading to React 19. @@ -1059,11 +1057,7 @@ export function buildHooks({ } const subscriptionRemoved = - !currentRenderHasSubscription && lastRenderHadSubscription.current - - usePossiblyImmediateEffect(() => { - lastRenderHadSubscription.current = currentRenderHasSubscription - }) + !currentRenderHasSubscription && promiseRef.current !== undefined usePossiblyImmediateEffect((): void | undefined => { if (subscriptionRemoved) { diff --git a/packages/toolkit/src/query/tests/buildHooks.test.tsx b/packages/toolkit/src/query/tests/buildHooks.test.tsx index 50ff561148..bfbcc72392 100644 --- a/packages/toolkit/src/query/tests/buildHooks.test.tsx +++ b/packages/toolkit/src/query/tests/buildHooks.test.tsx @@ -895,6 +895,71 @@ describe('hooks tests', () => { status: 'uninitialized', }) }) + + test('reset after unmount/remount', async () => { + const user = userEvent.setup() + + function QueryComponent() { + const { isLoading, data } = api.endpoints.getUser.useQuery(1) + + if (isLoading) { + return

Loading...

+ } + + return

{data?.name}

+ } + + function Wrapper() { + const [open, setOpen] = useState(true) + + const handleRerender = () => { + setOpen(false) + setTimeout(() => { + setOpen(true) + }, 1000) + } + + const handleReset = () => { + storeRef.store.dispatch(api.util.resetApiState()) + } + + return ( + <> + + {open ? ( +
+ + + +
+ ) : null} + + ) + } + + render(, { wrapper: storeRef.wrapper }) + + await user.click( + screen.getByRole('button', { name: /Rerender component/i }), + ) + await waitFor(() => { + expect(screen.getByText('Timmy')).toBeTruthy() + }) + + await user.click( + screen.getByRole('button', { name: /reset api state/i }), + ) + await waitFor(() => { + expect(screen.queryByText('Loading...')).toBeNull() + }) + await waitFor(() => { + expect(screen.getByText('Timmy')).toBeTruthy() + }) + }) }) test('useQuery refetch method returns a promise that resolves with the result', async () => { From 453ba917fb2598f162f78ef33c42480f382a0e58 Mon Sep 17 00:00:00 2001 From: "Junius Free B. Fontamillas" <8164667+juniusfree@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:27:25 +0800 Subject: [PATCH 2/2] Update packages/toolkit/src/query/tests/buildHooks.test.tsx Co-authored-by: Jackson Brummell --- packages/toolkit/src/query/tests/buildHooks.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/toolkit/src/query/tests/buildHooks.test.tsx b/packages/toolkit/src/query/tests/buildHooks.test.tsx index bfbcc72392..02be5d89fb 100644 --- a/packages/toolkit/src/query/tests/buildHooks.test.tsx +++ b/packages/toolkit/src/query/tests/buildHooks.test.tsx @@ -896,7 +896,7 @@ describe('hooks tests', () => { }) }) - test('reset after unmount/remount', async () => { +test('hook should not be stuck loading post resetApiState after re-render', async () => { const user = userEvent.setup() function QueryComponent() {