Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rtk-query): useQuery hook does not refetch after resetApiState #4758

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1035,8 +1035,6 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
skipPollingIfUnfocused,
})

const lastRenderHadSubscription = useRef(false)

// TODO: Change this to `useRef<QueryActionCreatorResult<any>>(undefined)` after upgrading to React 19.
/**
* @todo Change this to `useRef<QueryActionCreatorResult<any>>(undefined)` after upgrading to React 19.
Expand All @@ -1059,11 +1057,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
}

const subscriptionRemoved =
!currentRenderHasSubscription && lastRenderHadSubscription.current

usePossiblyImmediateEffect(() => {
lastRenderHadSubscription.current = currentRenderHasSubscription
})
!currentRenderHasSubscription && promiseRef.current !== undefined

usePossiblyImmediateEffect((): void | undefined => {
if (subscriptionRemoved) {
Expand Down
65 changes: 65 additions & 0 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,71 @@ describe('hooks tests', () => {
status: 'uninitialized',
})
})

test('hook should not be stuck loading post resetApiState after re-render', async () => {
const user = userEvent.setup()

function QueryComponent() {
const { isLoading, data } = api.endpoints.getUser.useQuery(1)

if (isLoading) {
return <p>Loading...</p>
}

return <p>{data?.name}</p>
}

function Wrapper() {
const [open, setOpen] = useState(true)

const handleRerender = () => {
setOpen(false)
setTimeout(() => {
setOpen(true)
}, 1000)
}

const handleReset = () => {
storeRef.store.dispatch(api.util.resetApiState())
}

return (
<>
<button onClick={handleRerender} aria-label="Rerender component">
Rerender
</button>
{open ? (
<div>
<button onClick={handleReset} aria-label="Reset API state">
Reset
</button>

<QueryComponent />
</div>
) : null}
</>
)
}

render(<Wrapper />, { 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 () => {
Expand Down
Loading