Skip to content

fix: show previous data while loading new one #121

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 10 additions & 1 deletion src/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ApolloClient, {
WatchQueryOptions,
} from 'apollo-client';
import { DocumentNode } from 'graphql';
import { useContext, useEffect, useMemo, useState } from 'react';
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
import { useApolloClient } from './ApolloContext';
import { SSRContext } from './internal/SSRContext';
import actHack from './internal/actHack';
Expand Down Expand Up @@ -96,6 +96,8 @@ export function useQuery<
? 'cache-first'
: actualCachePolicy;

const lastResult = useRef(null);

const watchQueryOptions: WatchQueryOptions<TVariables> = useMemo(
() =>
compact({
Expand Down Expand Up @@ -144,6 +146,12 @@ export function useQuery<
};
}

if (result.loading) {
data = {
...(lastResult.current || {}).data,
};
}

return {
data,
error:
Expand All @@ -161,6 +169,7 @@ export function useQuery<
},
[shouldSkip, responseId, observableQuery]
);
lastResult.current = currentResult;

useEffect(
() => {
Expand Down