diff --git a/.changeset/funny-pugs-tan.md b/.changeset/funny-pugs-tan.md new file mode 100644 index 0000000000..1dc312dc6b --- /dev/null +++ b/.changeset/funny-pugs-tan.md @@ -0,0 +1,5 @@ +--- +'urql': patch +--- + +Update `useQuery` implementation to avoid an aborted render on initial mount. We abort a render-on-update once when the state needs to be updated according to the `OperationResult` source we need to listen to and execute. However, we can avoid this on the initial mount as we've done in a prior version. This fix **does not** change any of the current behaviour, but simply avoids the confusing state transition on mount. diff --git a/packages/react-urql/src/hooks/useQuery.ts b/packages/react-urql/src/hooks/useQuery.ts index 94b98b0907..06565f5105 100644 --- a/packages/react-urql/src/hooks/useQuery.ts +++ b/packages/react-urql/src/hooks/useQuery.ts @@ -53,17 +53,30 @@ export function useQuery( const request = useRequest(args.query, args.variables); const cache = getCacheForClient(client); + const currDeps: unknown[] = [ + client, + request, + args.pause, + args.requestPolicy, + args.context, + ]; + const [meta, setMeta] = useState<{ source: Source> | null; prevValue: UseQueryState; - deps: Array; + deps: unknown[]; suspense: boolean; - }>({ - source: null, + }>(() => ({ + source: args.pause + ? null + : client.executeQuery(request, { + requestPolicy: args.requestPolicy, + ...args.context, + }), prevValue: notFetching, - deps: [], + deps: currDeps, suspense: isSuspense(client, args.context), - }); + })); const { source, deps, suspense } = meta; @@ -167,23 +180,15 @@ export function useQuery( ) )); - const currDeps = [ - client, - request, - args.pause, - args.requestPolicy, - args.context, - ]; - if (hasDepsChanged(deps, currDeps) && !args.pause) { - const fetchSource = client.executeQuery(request, { - requestPolicy: args.requestPolicy, - ...args.context, - }); - setMeta({ prevValue: result, - source: args.pause ? null : fetchSource, + source: args.pause + ? null + : client.executeQuery(request, { + requestPolicy: args.requestPolicy, + ...args.context, + }), deps: currDeps, suspense: isSuspense(client, args.context), });