T
TanStack2y ago
stormy-gold

How to implement a callback/action when the query is successful?

Hi there previously I did something like this
// On success, omgevingscheck data is saved in cache and in ctx.
export function useOmgevingscheck({ wkt, verzoekAard }: IBodyProps) {
const setOmgevingscheckResultInCtx = useSetOmgevingscheckResult();

return useQuery({
queryKey: ['useOmgevingscheck', { wkt, verzoekAard }],
queryFn: () => getOmgevingscheck({ wkt, verzoekAard }),
enabled: !!wkt,
// [FIXME]: Replace onSuccess want het is deprecated
onSuccess: (data: IOmgevingscheckResult) => {
setOmgevingscheckResultInCtx(data);
},
});
}
// On success, omgevingscheck data is saved in cache and in ctx.
export function useOmgevingscheck({ wkt, verzoekAard }: IBodyProps) {
const setOmgevingscheckResultInCtx = useSetOmgevingscheckResult();

return useQuery({
queryKey: ['useOmgevingscheck', { wkt, verzoekAard }],
queryFn: () => getOmgevingscheck({ wkt, verzoekAard }),
enabled: !!wkt,
// [FIXME]: Replace onSuccess want het is deprecated
onSuccess: (data: IOmgevingscheckResult) => {
setOmgevingscheckResultInCtx(data);
},
});
}
but onSuccess and onError have been deprecated, what do I need to use in the v5 api to arrive at the same functionality? Thanks
4 Replies
foreign-sapphire
foreign-sapphire2y ago
please don't sync state - you can just use data returned from useQuery
stormy-gold
stormy-goldOP2y ago
hey @TkDodo 🔮 what if I would like to set that data returned from the query on my context? or is this a no no?
foreign-sapphire
foreign-sapphire2y ago
React Query and React Context
Can it make sense to combine React Query with React Context ? Yes, sometimes ...
stormy-gold
stormy-goldOP2y ago
aah thanks very much 🙏

Did you find this page helpful?