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

(react) - Update initial state to not require abort on mount #2227

Merged
merged 2 commits into from
Jan 31, 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
5 changes: 5 additions & 0 deletions .changeset/funny-pugs-tan.md
Original file line number Diff line number Diff line change
@@ -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.
43 changes: 24 additions & 19 deletions packages/react-urql/src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,30 @@ export function useQuery<Data = any, Variables = object>(
const request = useRequest<Data, Variables>(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<OperationResult<Data, Variables>> | null;
prevValue: UseQueryState<Data, Variables>;
deps: Array<any>;
deps: unknown[];
suspense: boolean;
}>({
source: null,
}>(() => ({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't strictly speaking necessary, but let's avoid calling client.executeQuery on every render 😆

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;

Expand Down Expand Up @@ -167,23 +180,15 @@ export function useQuery<Data = any, Variables = object>(
)
));

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),
});
Expand Down