You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to modify the vars passed in a mutation inside onMutate or any other side effects that I'm not aware of?
Consider this mutation hook
typeVarType={lastCellId: number;position: number;}exportfunctionuseInsertCell(){constqueryClient=useQueryClient();returnuseMutationWithAuth(insertCell,{onMutate: async(vars: VarType)=>{awaitqueryClient.cancelQueries({queryKey: ["cells",vars.pageId],});//I will perform some sorting here based on the query data to recalculate the official position of my newly added cell based on lastCell idconstcachedCells: Cell[]=queryClient.getQueryData(["cells",vars.pageId])||[];constnewCell={...vars,id: undefined}//Pretending the calculation is done//I want to update var.position here//I understand that this can be mutated, but isn't that an anti pattern?vars.position=cachedCells.find(c=>c.id===undefined).position;return{previousCells: queryClient.getQueryData(["cells",vars.pageId,]),pageId: vars.pageId,};},});}
Is there anyway to "mutate" the mutation variable so I can send the correct payload to my API endpoint? Updating it directly seems like an anti pattern
Or do I have no choice but to do all of it before the mutation? I'm essentially trying to avoid using query client on my components, for SOC purposes.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Is it possible to modify the vars passed in a mutation inside onMutate or any other side effects that I'm not aware of?
Consider this mutation hook
Is there anyway to "mutate" the mutation variable so I can send the correct payload to my API endpoint? Updating it directly seems like an anti pattern
Or do I have no choice but to do all of it before the mutation? I'm essentially trying to avoid using query client on my components, for SOC purposes.
Beta Was this translation helpful? Give feedback.
All reactions