Skip to content

Commit

Permalink
(react) - Update initial state to not require abort on mount
Browse files Browse the repository at this point in the history
Previously, the `useSyncExternalStore` reimplementation regressed
on us constructing the correct initial state on mount, and instead
relied on an abort on mount, which is confusing to users.
Instead, we can simply revert to what we've done prior to the
change and ensure that the initial state is accurate.
  • Loading branch information
kitten committed Jan 31, 2022
1 parent 481bf43 commit b54b7d1
Showing 1 changed file with 24 additions and 19 deletions.
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,
}>(() => ({
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

0 comments on commit b54b7d1

Please sign in to comment.