Skip to content

Commit 5328dae

Browse files
committed
move onCompleted and onError to the snapshot function
1 parent 5635437 commit 5328dae

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

src/react/hooks/useQuery.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ export function useQuery<
173173
}
174174

175175
previousResult = result;
176+
177+
if (!result.loading) {
178+
if (result.data) {
179+
ref.current.options?.onCompleted?.(result.data);
180+
} else if (result.error) {
181+
ref.current.options?.onError?.(result.error);
182+
}
183+
}
176184
}
177185

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

193201
let result = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
194-
useEffect(() => {
195-
if (
196-
ref.current.options?.skip ||
197-
ref.current.options?.fetchPolicy === 'standby'
198-
) {
199-
return;
200-
}
201-
202-
if (!result.loading) {
203-
if (result.error) {
204-
ref.current.options?.onError?.(result.error);
205-
} else if (result.data) {
206-
ref.current.options?.onCompleted?.(result.data);
207-
}
208-
}
209-
}, [result]);
210-
211202
let partial: boolean | undefined;
212203
({ partial, ...result } = result);
213204
if (options?.skip || options?.fetchPolicy === 'standby') {

0 commit comments

Comments
 (0)