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 skipToken behaviour in useQueryState #2779

Merged
merged 1 commit into from
Oct 18, 2022
Merged
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
13 changes: 9 additions & 4 deletions packages/toolkit/src/query/core/buildSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,19 @@ export function buildSlice({
draft[queryCacheKey]![requestId] = options
}
return true
} else if (unsubscribeQueryResult.match(action)) {
}
if (unsubscribeQueryResult.match(action)) {
const { queryCacheKey, requestId } = action.payload
if (draft[queryCacheKey]) {
delete draft[queryCacheKey]![requestId]
}
return true
} else if (querySlice.actions.removeQueryResult.match(action)) {
}
if (querySlice.actions.removeQueryResult.match(action)) {
delete draft[action.payload.queryCacheKey]
} else if (queryThunk.pending.match(action)) {
return true
}
if (queryThunk.pending.match(action)) {
const {
meta: { arg, requestId },
} = action
Expand All @@ -447,7 +451,8 @@ export function buildSlice({

return true
}
} else if (queryThunk.rejected.match(action)) {
}
if (queryThunk.rejected.match(action)) {
const {
meta: { condition, arg, requestId },
} = action
Expand Down
4 changes: 3 additions & 1 deletion packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,9 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
)
lastResult = undefined
}

if (queryArgs === skipToken) {
lastResult = undefined
}
// data is the last known good request result we have tracked - or if none has been tracked yet the last good result for the current args
let data = currentState.isSuccess ? currentState.data : lastResult?.data
if (data === undefined) data = currentState.data
Expand Down
12 changes: 2 additions & 10 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2379,11 +2379,7 @@ describe('skip behaviour', () => {
await act(async () => {
rerender([1, { skip: true }])
})
expect(result.current).toEqual({
...uninitialized,
currentData: undefined,
data: { name: 'Timmy' },
})
expect(result.current).toEqual(uninitialized)
await delay(1)
expect(subscriptionCount('getUser(1)')).toBe(0)
})
Expand Down Expand Up @@ -2415,11 +2411,7 @@ describe('skip behaviour', () => {
await act(async () => {
rerender([skipToken])
})
expect(result.current).toEqual({
...uninitialized,
currentData: undefined,
data: { name: 'Timmy' },
})
expect(result.current).toEqual(uninitialized)
await delay(1)
expect(subscriptionCount('getUser(1)')).toBe(0)
})
Expand Down