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

feat(queryObserver): add isInitialLoading derived flag #4244

Merged
merged 5 commits into from
Sep 30, 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
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ jobs:
- uses: pnpm/[email protected]
with:
version: 7
- uses: actions/setup-node@v2
- uses: actions/setup-node@v3
with:
node-version: 16.14.2
registry-url: https://registry.npmjs.org/
cache: 'pnpm'
- run: |
pnpm --filter "./packages/**" --filter query install
pnpm --filter "./packages/**" --filter query --prefer-offline install
git config --global user.name 'Tanner Linsley'
git config --global user.email '[email protected]'
npm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"
pnpm run cipublish
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
- uses: pnpm/[email protected]
with:
version: 7
- uses: actions/setup-node@v2
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: 'pnpm'
- name: Install dependencies
run: pnpm --filter "./packages/**" --filter query install
run: pnpm --filter "./packages/**" --filter query --prefer-offline install
- run: |
pnpm run test:ci
pnpm run test:size
Expand Down
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

18 changes: 14 additions & 4 deletions docs/guides/disabling-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ When `enabled` is `false`:
```tsx
function Todos() {
const {
isLoading,
isInitialLoading,
isError,
data,
error,
Expand All @@ -45,10 +45,10 @@ function Todos() {
isError ? (
<span>Error: {error.message}</span>
) : (
(isLoading && !isFetching) ? (
<span>Not ready ...</span>
isInitialLoading ? (
<span>Loading...</span>
) : (
<span>Loading...</span>
<span>Not ready ...</span>
)
)
)}
Expand Down Expand Up @@ -87,3 +87,13 @@ function Todos() {
)
}
```

### isInitialLoading

Lazy queries will be in `status: 'loading'` right from the start because `loading` means that there is no data yet. This is technically true, however, since we are not currently fetching any data (as the query is not _enabled_), it also means you likely cannot use this flag to show a loading spinner.

If you are using disabled or lazy queries, you can use the `isInitialLoading` flag instead. It's a derived flag that is computed from:

`isLoading && isFetching`

so it will only be true if the query is currently fetching for the first time.
11 changes: 11 additions & 0 deletions docs/guides/migrating-to-react-query-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ This will mostly affect `disabled` queries that don't have any `data` yet, as th

Also, have a look at [the guide on dependent queries](../guides/dependent-queries)

#### disabled queries

Due to this change, disabled queries (even temporarily disabled ones) will start in `loading` state. To make migration easier, especially for having a good flag to know when to display a loading spinner, you can check for `isInitialLoading` instead of `isLoading`:

```diff
- isLoading
+ isInitialLoading
```

See also the guide on [disabling queries](../guides/disabling-queries#isInitialLoading)

### new API for `useQueries`

The `useQueries` hook now accepts an object with a `queries` prop as its input. The value of the `queries` prop is an array of queries (this array is identical to what was passed into `useQueries` in v3).
Expand Down
3 changes: 3 additions & 0 deletions docs/reference/useQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ const result = useQuery({
- `isRefetching: boolean`
- Is `true` whenever a background refetch is in-flight, which _does not_ include initial `loading`
- Is the same as `isFetching && !isLoading`
- `isInitialLoading: boolean`
- Is `true` whenever the first fetch for a query is in-flight
- Is the same as `isFetching && isLoading`
- `failureCount: number`
- The failure count for the query.
- Incremented every time the query fails.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"bundlewatch": {
"files": [
{
"path": "packages/**/build/umd/*.production.js"
"path": "packages/*/build/umd/*.production.js"
}
]
}
Expand Down
15 changes: 9 additions & 6 deletions packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,16 @@ export class QueryObserver<
}

const isFetching = fetchStatus === 'fetching'
const isLoading = status === 'loading'
const isError = status === 'error'

const result: QueryObserverBaseResult<TData, TError> = {
status,
fetchStatus,
isLoading: status === 'loading',
isLoading,
isSuccess: status === 'success',
isError: status === 'error',
isError,
isInitialLoading: isLoading && isFetching,
data,
dataUpdatedAt,
error,
Expand All @@ -564,13 +567,13 @@ export class QueryObserver<
isFetchedAfterMount:
state.dataUpdateCount > queryInitialState.dataUpdateCount ||
state.errorUpdateCount > queryInitialState.errorUpdateCount,
isFetching: isFetching,
isRefetching: isFetching && status !== 'loading',
isLoadingError: status === 'error' && state.dataUpdatedAt === 0,
isFetching,
isRefetching: isFetching && !isLoading,
isLoadingError: isError && state.dataUpdatedAt === 0,
isPaused: fetchStatus === 'paused',
isPlaceholderData,
isPreviousData,
isRefetchError: status === 'error' && state.dataUpdatedAt !== 0,
isRefetchError: isError && state.dataUpdatedAt !== 0,
isStale: isStale(query, options),
refetch: this.refetch,
remove: this.remove,
Expand Down
1 change: 1 addition & 0 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ export interface QueryObserverBaseResult<TData = unknown, TError = unknown> {
isFetching: boolean
isLoading: boolean
isLoadingError: boolean
isInitialLoading: boolean
isPaused: boolean
isPlaceholderData: boolean
isPreviousData: boolean
Expand Down
2 changes: 2 additions & 0 deletions packages/react-query/src/__tests__/useInfiniteQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('useInfiniteQuery', () => {
isFetchingNextPage: false,
isFetchingPreviousPage: false,
isLoading: true,
isInitialLoading: true,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand Down Expand Up @@ -116,6 +117,7 @@ describe('useInfiniteQuery', () => {
isFetchingNextPage: false,
isFetchingPreviousPage: false,
isLoading: false,
isInitialLoading: false,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand Down
5 changes: 5 additions & 0 deletions packages/react-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ describe('useQuery', () => {
isFetching: true,
isPaused: false,
isLoading: true,
isInitialLoading: true,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand All @@ -266,6 +267,7 @@ describe('useQuery', () => {
isFetching: false,
isPaused: false,
isLoading: false,
isInitialLoading: false,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand Down Expand Up @@ -322,6 +324,7 @@ describe('useQuery', () => {
isFetching: true,
isPaused: false,
isLoading: true,
isInitialLoading: true,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand All @@ -348,6 +351,7 @@ describe('useQuery', () => {
isFetching: true,
isPaused: false,
isLoading: true,
isInitialLoading: true,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand All @@ -374,6 +378,7 @@ describe('useQuery', () => {
isFetching: false,
isPaused: false,
isLoading: false,
isInitialLoading: false,
isLoadingError: true,
isPlaceholderData: false,
isPreviousData: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe('useInfiniteQuery', () => {
isFetchingNextPage: false,
isFetchingPreviousPage: false,
isLoading: true,
isInitialLoading: true,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand Down Expand Up @@ -125,6 +126,7 @@ describe('useInfiniteQuery', () => {
isFetchingNextPage: false,
isFetchingPreviousPage: false,
isLoading: false,
isInitialLoading: false,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand Down
5 changes: 5 additions & 0 deletions packages/solid-query/src/__tests__/createQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ describe('createQuery', () => {
isFetching: true,
isPaused: false,
isLoading: true,
isInitialLoading: true,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand All @@ -299,6 +300,7 @@ describe('createQuery', () => {
isFetching: false,
isPaused: false,
isLoading: false,
isInitialLoading: false,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand Down Expand Up @@ -361,6 +363,7 @@ describe('createQuery', () => {
isFetching: true,
isPaused: false,
isLoading: true,
isInitialLoading: true,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand All @@ -387,6 +390,7 @@ describe('createQuery', () => {
isFetching: true,
isPaused: false,
isLoading: true,
isInitialLoading: true,
isLoadingError: false,
isPlaceholderData: false,
isPreviousData: false,
Expand All @@ -413,6 +417,7 @@ describe('createQuery', () => {
isFetching: false,
isPaused: false,
isLoading: false,
isInitialLoading: false,
isLoadingError: true,
isPlaceholderData: false,
isPreviousData: false,
Expand Down