Skip to content

Commit 5e7f47b

Browse files
committed
fix: show previous data while loading new one
1 parent ac6da2c commit 5e7f47b

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/useQuery.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import ApolloClient, {
1212
WatchQueryOptions,
1313
} from 'apollo-client';
1414
import { DocumentNode } from 'graphql';
15-
import { useContext, useEffect, useMemo, useState } from 'react';
15+
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
1616
import { useApolloClient } from './ApolloContext';
1717
import { SSRContext } from './internal/SSRContext';
1818
import actHack from './internal/actHack';
@@ -96,6 +96,8 @@ export function useQuery<
9696
? 'cache-first'
9797
: actualCachePolicy;
9898

99+
const lastResult = useRef(null);
100+
99101
const watchQueryOptions: WatchQueryOptions<TVariables> = useMemo(
100102
() =>
101103
compact({
@@ -144,6 +146,12 @@ export function useQuery<
144146
};
145147
}
146148

149+
if (result.loading) {
150+
data = {
151+
...(lastResult.current || {}).data,
152+
};
153+
}
154+
147155
return {
148156
data,
149157
error:
@@ -161,6 +169,7 @@ export function useQuery<
161169
},
162170
[shouldSkip, responseId, observableQuery]
163171
);
172+
lastResult.current = currentResult;
164173

165174
useEffect(
166175
() => {

0 commit comments

Comments
 (0)