Skip to content

Commit 4df81f8

Browse files
committed
2 parents 6d4eeb8 + 6864624 commit 4df81f8

File tree

13 files changed

+372
-111
lines changed

13 files changed

+372
-111
lines changed

docs/src/manifests/manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
"title": "TypeScript",
4343
"path": "/docs/typescript",
4444
"editUrl": "/docs/typescript.md"
45+
},
46+
{
47+
"title": "React Native",
48+
"path": "/docs/react-native",
49+
"editUrl": "/docs/react-native.md"
4550
}
4651
]
4752
},

docs/src/pages/docs/api.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ const {
1212
error,
1313
failureCount,
1414
isError,
15+
isFetchedAfterMount,
1516
isFetching,
1617
isIdle,
1718
isLoading,
19+
isPreviousData,
1820
isStale,
1921
isSuccess,
2022
refetch,
2123
status,
2224
} = useQuery(queryKey, queryFn?, {
2325
cacheTime,
2426
enabled,
27+
forceFetchOnMount,
2528
initialData,
2629
initialStale,
2730
isDataEqual,
@@ -99,8 +102,7 @@ const queryInfo = useQuery({
99102
- Set this to `true` or `false` to enable/disable automatic refetching on reconnect for this query.
100103
- `notifyOnStatusChange: Boolean`
101104
- Optional
102-
- Whether a change to the query status should re-render a component.
103-
- If set to `false`, the component will only re-render when the actual `data` or `error` changes.
105+
- Set this to `false` to only re-render when there are changes to `data` or `error`.
104106
- Defaults to `true`.
105107
- `onSuccess: Function(data) => data`
106108
- Optional
@@ -128,6 +130,10 @@ const queryInfo = useQuery({
128130
- Optional
129131
- Defaults to `false`
130132
- If set, any previous `data` will be kept when fetching new data because the query key changed.
133+
- `forceFetchOnMount: Boolean`
134+
- Optional
135+
- Defaults to `false`
136+
- Set this to `true` to always fetch when the component mounts (regardless of staleness).
131137
- `refetchOnMount: Boolean`
132138
- Optional
133139
- Defaults to `true`
@@ -165,6 +171,11 @@ const queryInfo = useQuery({
165171
- The error object for the query, if an error was thrown.
166172
- `isStale: Boolean`
167173
- Will be `true` if the cache data is stale.
174+
- `isPreviousData: Boolean`
175+
- Will be `true` when `keepPreviousData` is set and data from the previous query is returned.
176+
- `isFetchedAfterMount: Boolean`
177+
- Will be `true` if the query has been fetched after the component mounted.
178+
- This property can be used to not show any previously cached data.
168179
- `isFetching: Boolean`
169180
- Defaults to `true` so long as `manual` is set to `false`
170181
- Will be `true` if the query is currently fetching, including background fetching.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
id: react-native
3+
title: React Native
4+
---
5+
6+
When using React Query on React Native you may face the following error:
7+
8+
> React Native throws fullscreen error when promises are rejected
9+
10+
This is happening because by default React Query uses `console.error` to log promise rejection.
11+
12+
To fix this issue you can replace `console.error` with `console.warn` using [setConsole](https://react-query.tanstack.com/docs/api#setconsole).
13+
14+
```js
15+
import { setConsole } from 'react-query'
16+
17+
setConsole({
18+
log: console.log,
19+
warn: console.warn,
20+
error: console.warn,
21+
})
22+
```

src/core/config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ export const DEFAULT_STALE_TIME = 0
5151
export const DEFAULT_CACHE_TIME = 5 * 60 * 1000
5252
export const DEFAULT_CONFIG: ReactQueryConfig = {
5353
queries: {
54-
queryKeySerializerFn: defaultQueryKeySerializerFn,
54+
cacheTime: DEFAULT_CACHE_TIME,
5555
enabled: true,
56+
notifyOnStatusChange: true,
57+
queryKeySerializerFn: defaultQueryKeySerializerFn,
58+
refetchOnMount: true,
59+
refetchOnReconnect: true,
60+
refetchOnWindowFocus: true,
5661
retry: 3,
5762
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, 30000),
5863
staleTime: DEFAULT_STALE_TIME,
59-
cacheTime: DEFAULT_CACHE_TIME,
60-
refetchOnWindowFocus: true,
61-
refetchOnReconnect: true,
62-
refetchOnMount: true,
63-
notifyOnStatusChange: true,
6464
structuralSharing: true,
6565
},
6666
}

src/core/query.ts

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
isDocumentVisible,
1010
isOnline,
1111
isServer,
12+
noop,
1213
replaceEqualDeep,
1314
sleep,
1415
} from './utils'
@@ -38,6 +39,7 @@ export interface QueryState<TResult, TError> {
3839
data?: TResult
3940
error: TError | null
4041
failureCount: number
42+
fetchedCount: number
4143
isError: boolean
4244
isFetched: boolean
4345
isFetching: boolean
@@ -135,7 +137,7 @@ export class Query<TResult, TError> {
135137
this.state = queryReducer(this.state, action)
136138

137139
this.observers.forEach(observer => {
138-
observer.onQueryUpdate(this.state, action)
140+
observer.onQueryUpdate(action)
139141
})
140142

141143
this.notifyGlobalListeners(this)
@@ -157,20 +159,6 @@ export class Query<TResult, TError> {
157159
}, this.cacheTime)
158160
}
159161

160-
async refetch(
161-
options?: RefetchOptions,
162-
config?: QueryConfig<TResult, TError>
163-
): Promise<TResult | undefined> {
164-
try {
165-
return await this.fetch(undefined, config)
166-
} catch (error) {
167-
if (options?.throwOnError === true) {
168-
throw error
169-
}
170-
return undefined
171-
}
172-
}
173-
174162
cancel(): void {
175163
this.cancelFetch?.()
176164
}
@@ -252,8 +240,9 @@ export class Query<TResult, TError> {
252240
observer.config.refetchOnWindowFocus
253241
)
254242
) {
255-
this.fetch()
243+
this.fetch().catch(noop)
256244
}
245+
257246
this.continue()
258247
}
259248

@@ -266,8 +255,9 @@ export class Query<TResult, TError> {
266255
observer.config.refetchOnReconnect
267256
)
268257
) {
269-
this.fetch()
258+
this.fetch().catch(noop)
270259
}
260+
271261
this.continue()
272262
}
273263

@@ -306,6 +296,35 @@ export class Query<TResult, TError> {
306296
this.scheduleGc()
307297
}
308298

299+
async refetch(
300+
options?: RefetchOptions,
301+
config?: QueryConfig<TResult, TError>
302+
): Promise<TResult | undefined> {
303+
try {
304+
return await this.fetch(undefined, config)
305+
} catch (error) {
306+
if (options?.throwOnError === true) {
307+
throw error
308+
}
309+
}
310+
}
311+
312+
async fetchMore(
313+
fetchMoreVariable?: unknown,
314+
options?: FetchMoreOptions,
315+
config?: QueryConfig<TResult, TError>
316+
): Promise<TResult | undefined> {
317+
return this.fetch(
318+
{
319+
fetchMore: {
320+
fetchMoreVariable,
321+
previous: options?.previous || false,
322+
},
323+
},
324+
config
325+
)
326+
}
327+
309328
async fetch(
310329
options?: FetchOptions,
311330
config?: QueryConfig<TResult, TError>
@@ -548,22 +567,6 @@ export class Query<TResult, TError> {
548567
run()
549568
})
550569
}
551-
552-
fetchMore(
553-
fetchMoreVariable?: unknown,
554-
options?: FetchMoreOptions,
555-
config?: QueryConfig<TResult, TError>
556-
): Promise<TResult | undefined> {
557-
return this.fetch(
558-
{
559-
fetchMore: {
560-
fetchMoreVariable,
561-
previous: options?.previous || false,
562-
},
563-
},
564-
config
565-
)
566-
}
567570
}
568571

569572
function getLastPage<TResult>(pages: TResult[], previous?: boolean): TResult {
@@ -578,7 +581,6 @@ function hasMorePages<TResult, TError>(
578581
if (config.infinite && config.getFetchMore && Array.isArray(pages)) {
579582
return Boolean(config.getFetchMore(getLastPage(pages, previous), pages))
580583
}
581-
return undefined
582584
}
583585

584586
function getDefaultState<TResult, TError>(
@@ -604,6 +606,7 @@ function getDefaultState<TResult, TError>(
604606
isFetching: initialStatus === QueryStatus.Loading,
605607
isFetchingMore: false,
606608
failureCount: 0,
609+
fetchedCount: 0,
607610
data: initialData,
608611
updatedAt: Date.now(),
609612
canFetchMore: hasMorePages(config, initialData),
@@ -638,6 +641,7 @@ export function queryReducer<TResult, TError>(
638641
...getStatusProps(QueryStatus.Success),
639642
data: action.data,
640643
error: null,
644+
fetchedCount: state.fetchedCount + 1,
641645
isFetched: true,
642646
isFetching: false,
643647
isFetchingMore: false,
@@ -650,6 +654,7 @@ export function queryReducer<TResult, TError>(
650654
...state,
651655
...getStatusProps(QueryStatus.Error),
652656
error: action.error,
657+
fetchedCount: state.fetchedCount + 1,
653658
isFetched: true,
654659
isFetching: false,
655660
isFetchingMore: false,

0 commit comments

Comments
 (0)