Skip to content

Commit 00a77aa

Browse files
authored
Merge pull request #1 from acontreras89/feature/useQuery-reset
Add docs and tests for reset method in useQuery
2 parents 70ce58b + 0326015 commit 00a77aa

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

docs/src/pages/reference/useQuery.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const {
2424
isSuccess,
2525
refetch,
2626
remove,
27+
reset,
2728
status,
2829
} = useQuery(queryKey, queryFn?, {
2930
cacheTime,
@@ -228,3 +229,6 @@ const result = useQuery({
228229
- If `cancelRefetch` is `true`, then the current request will be cancelled before a new request is made
229230
- `remove: () => void`
230231
- A function to remove the query from the cache.
232+
- `reset: (options: { throwOnError: boolean, cancelRefetch: boolean }) => Promise<UseQueryResult>`
233+
- A function to manually reset the query to its initial state.
234+
- `options` are used to call `refetch` on the query after it is resetted.

src/react/tests/useQuery.test.tsx

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,82 @@ describe('useQuery', () => {
10321032
expect(states[3]).toMatchObject({ data: 2 })
10331033
})
10341034

1035+
it('should be able to reset a query', async () => {
1036+
const key = queryKey()
1037+
const states: UseQueryResult<number>[] = []
1038+
let count = 0
1039+
1040+
function Page() {
1041+
const state = useQuery(key, () => ++count)
1042+
1043+
states.push(state)
1044+
1045+
const { reset } = state
1046+
1047+
React.useEffect(() => {
1048+
setActTimeout(() => {
1049+
reset()
1050+
}, 5)
1051+
}, [reset])
1052+
1053+
return null
1054+
}
1055+
1056+
renderWithClient(queryClient, <Page />)
1057+
1058+
await sleep(20)
1059+
1060+
expect(states.length).toBe(5)
1061+
// Initial
1062+
expect(states[0]).toMatchObject({ data: undefined })
1063+
// Fetched
1064+
expect(states[1]).toMatchObject({ data: 1 })
1065+
// Reset
1066+
expect(states[2]).toMatchObject({ data: undefined })
1067+
// Initial (refetch)
1068+
expect(states[3]).toMatchObject({ data: undefined })
1069+
// Fetched
1070+
expect(states[4]).toMatchObject({ data: 2 })
1071+
})
1072+
1073+
it('should reset the query to its initial state', async () => {
1074+
const key = queryKey()
1075+
const states: UseQueryResult<number>[] = []
1076+
let count = 0
1077+
1078+
function Page() {
1079+
const state = useQuery(key, () => ++count, { initialData: 99 })
1080+
1081+
states.push(state)
1082+
1083+
const { reset } = state
1084+
1085+
React.useEffect(() => {
1086+
setActTimeout(() => {
1087+
reset()
1088+
}, 5)
1089+
}, [reset])
1090+
1091+
return null
1092+
}
1093+
1094+
renderWithClient(queryClient, <Page />)
1095+
1096+
await sleep(20)
1097+
1098+
expect(states.length).toBe(5)
1099+
// Initial
1100+
expect(states[0]).toMatchObject({ data: 99 })
1101+
// Fetched
1102+
expect(states[1]).toMatchObject({ data: 1 })
1103+
// Reset
1104+
expect(states[2]).toMatchObject({ data: 99 })
1105+
// Initial (refetch)
1106+
expect(states[3]).toMatchObject({ data: 99 })
1107+
// Fetched
1108+
expect(states[4]).toMatchObject({ data: 2 })
1109+
})
1110+
10351111
it('should share equal data structures between query results', async () => {
10361112
const key = queryKey()
10371113

0 commit comments

Comments
 (0)