Skip to content

Commit 76cb34a

Browse files
author
ismay
committed
fix(use-data-query): enable swr by default
BREAKING CHANGE: loading will only be set to true when fetching and if there is no data. If there is data, loading will be false during fetching. This means that stale data will be shown during fetches by default. If you'd like to opt out of showing stale data during loading you can use the new `fetching` attribute that is now returned by the useDataQuery hook instead.
1 parent a1a1f54 commit 76cb34a

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

services/data/src/react/hooks/useDataQuery.test.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { renderHook, act } from '@testing-library/react-hooks'
2-
import React from 'react'
2+
import * as React from 'react'
33
import { CustomDataProvider } from '../components/CustomDataProvider'
44
import { useDataQuery } from './useDataQuery'
55

@@ -471,7 +471,8 @@ describe('useDataQuery', () => {
471471

472472
expect(mockSpy).toHaveBeenCalledTimes(2)
473473
expect(result.current).toMatchObject({
474-
loading: true,
474+
loading: false,
475+
fetching: true,
475476
called: true,
476477
data: { x: 42 },
477478
})
@@ -481,6 +482,7 @@ describe('useDataQuery', () => {
481482
expect(mockSpy).toHaveBeenCalledTimes(2)
482483
expect(result.current).toMatchObject({
483484
loading: false,
485+
fetching: false,
484486
called: true,
485487
data: { x: 43 },
486488
})

services/data/src/react/hooks/useDataQuery.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,18 @@ export const useDataQuery = (
7373
const queryKey = [staticQuery, variables]
7474
const queryFn = () => engine.query(staticQuery, { variables })
7575

76-
const { isIdle, isFetching, error, data, refetch: queryRefetch } = useQuery(
77-
queryKey,
78-
queryFn,
79-
{
80-
enabled,
81-
onSuccess,
82-
onError,
83-
}
84-
)
76+
const {
77+
isIdle,
78+
isFetching,
79+
isLoading,
80+
error,
81+
data,
82+
refetch: queryRefetch,
83+
} = useQuery(queryKey, queryFn, {
84+
enabled,
85+
onSuccess,
86+
onError,
87+
})
8588

8689
/**
8790
* Refetch allows a user to update the variables or just
@@ -143,7 +146,8 @@ export const useDataQuery = (
143146
engine,
144147
// A query is idle if it is lazy and no initial data is available.
145148
called: !isIdle,
146-
loading: isFetching,
149+
loading: isLoading,
150+
fetching: isFetching,
147151
error: ourError,
148152
data,
149153
refetch,

services/data/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface ExecuteHookResult<ReturnType> {
4343
export interface QueryState {
4444
called: boolean
4545
loading: boolean
46+
fetching: boolean
4647
error?: FetchError
4748
data?: QueryResult
4849
}

0 commit comments

Comments
 (0)