Skip to content

Commit

Permalink
move onCompleted and onError to the snapshot function
Browse files Browse the repository at this point in the history
  • Loading branch information
brainkim committed Jan 14, 2022
1 parent 5635437 commit 5328dae
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ export function useQuery<
}

previousResult = result;

if (!result.loading) {
if (result.data) {
ref.current.options?.onCompleted?.(result.data);
} else if (result.error) {
ref.current.options?.onError?.(result.error);
}
}
}

return previousResult;
Expand All @@ -191,23 +199,6 @@ export function useQuery<
}), [obsQuery]);

let result = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
useEffect(() => {
if (
ref.current.options?.skip ||
ref.current.options?.fetchPolicy === 'standby'
) {
return;
}

if (!result.loading) {
if (result.error) {
ref.current.options?.onError?.(result.error);
} else if (result.data) {
ref.current.options?.onCompleted?.(result.data);
}
}
}, [result]);

let partial: boolean | undefined;
({ partial, ...result } = result);
if (options?.skip || options?.fetchPolicy === 'standby') {
Expand Down

0 comments on commit 5328dae

Please sign in to comment.