T
TanStack2y ago
stormy-gold

onSuccess migration: what to do for this use case?

Hey friends, I read TkDodos blog about killing the onSuccess APIs and that seems great, I'd love to hop on board. Everything he writes about makes a good amount of sense and I totally see the overarching reason -- onSuccess for useQuery creates a lot of anti-patterns. There is one use case I still can't wrap my head around -- creating arbitrary values for client side rendering in server side data. I have this state for ingredients, which, from the client, accepts a "selection". The database does not need to know, and should not know this information. The client messes with it and we do something with that client side state later. I basically do this: onSuccess: (data)=>{ ...data, selection: 2, other_clientside_info: 'hi' } how can I give my server side data arbitrary values like this? Thanks!
2 Replies
fair-rose
fair-rose2y ago
select instead of onSuccess? Or derive from data e.g const myData = { ...query.data, selection: 2} ? Hard to say without the full picture but likely one of those I think .. Maybe your selection should be it's own state even and not mixed with the query
xenogeneic-maroon
xenogeneic-maroon2y ago
it depends if that data is "static" - then select or derive is the way to go. If you need it in "state" because you change it on the client afterwards, you again have essentially two options that I'm describing here: https://tkdodo.eu/blog/react-query-and-forms
React Query and Forms
Forms tend to blur the line between server and client state, so let's see how that plays together with React Query.

Did you find this page helpful?