Skip to content

Commit 53e8424

Browse files
authored
fix: disabling query in suspense mode (TanStack#643)
1 parent ac2a21f commit 53e8424

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/react/tests/suspense.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,29 @@ describe("useQuery's in Suspense mode", () => {
151151

152152
await waitFor(() => rendered.getByText('rendered'))
153153
})
154+
155+
it('should not call the queryFn when not enabled', async () => {
156+
const queryFn = jest.fn()
157+
queryFn.mockImplementation(() => sleep(10))
158+
159+
function Page() {
160+
const [enabled, setEnabled] = React.useState(false)
161+
useQuery(['test'], queryFn, { suspense: true, enabled })
162+
163+
return <button aria-label="fire" onClick={() => setEnabled(true)} />
164+
}
165+
166+
const rendered = render(
167+
<React.Suspense fallback="loading">
168+
<Page />
169+
</React.Suspense>
170+
)
171+
172+
expect(queryFn).toHaveBeenCalledTimes(0)
173+
174+
fireEvent.click(rendered.getByLabelText('fire'))
175+
176+
expect(queryFn).toHaveBeenCalledTimes(1)
177+
await waitFor(() => rendered.getByLabelText('fire'))
178+
})
154179
})

src/react/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ export function handleSuspense(queryInfo) {
6868
throw queryInfo.error
6969
}
7070

71-
if (queryInfo.query.config.suspense && queryInfo.status !== statusSuccess) {
71+
if (
72+
queryInfo.query.config.suspense &&
73+
queryInfo.status !== statusSuccess &&
74+
queryInfo.query.config.enabled
75+
) {
7276
queryInfo.query.wasSuspended = true
7377
throw queryInfo.query.fetch()
7478
}

0 commit comments

Comments
 (0)