-
ProblemLifecycle hooks are primarily used to orchestrate invalidation,optimistic updates and etc and heavily rely on current const qc = useQueryClient()
useQuery({
...params,
onSuccess: ()=>{
qc.invalidateQueries(...)
}
}) Proposed solutionProvide onSuccess: (data, variables, _,qc)=>{
qc.invalidateQueries(...)
} ExtraCould we provide an easier way to bundle callbacks? Right now if you want to encapsulate and reuse your queries, separating UX/Data logic in callbacks as well you have to resort to self-written callback merging. Example: internal onSuccess invalidates data, external pushes the router page. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi 👋 At face value, this seems like a nice thing to do 🙂 To address the extra question - this is partly why you can specify callbacks at query client, hook, and at mutation function level (in the case of mutations). It's a little easier with mutations as you can perform data/cache-related side effects in the hook callbacks, and UI-related side effects in the mutation function callbacks. Are you proposing we expose some form of |
Beta Was this translation helpful? Give feedback.
-
it's not easily possible to provide the let's try to solve the problem from a different angle. If you want automatic invalidation without accessing the client, this can work: |
Beta Was this translation helpful? Give feedback.
it's not easily possible to provide the
queryClient
to callbacks, because the client depends on usage, but a query / mutation is a separate entity. Theoretically, you can have the same query run under different providers with different clients. Yes, it would be two observers and the callbacks are on observer-level, so we could make that work, but I don't think we could pass the client to the cache level callbacks then, creating an incoherent api.let's try to solve the problem from a different angle. If you want automatic invalidation without accessing the client, this can work:
https://twitter.com/TkDodo/status/1630321212013387776