T
TanStack3y ago
extended-salmon

context returns null on error so I was not able to go tot previous stage

Hi All, I am using useMutation in react. I want to implement optimistic update.It is working fine when it is success. When error occures I could not reset to previous value as I am getting context as undefined in the event of error. appreciate helping to solve this ... const {isLoading,mutate} = useMutation({ mutationFn : (userRole) => query.updateRole(userRole.userId,userRole.roleId), onMutate : (userRole) => setRole(userRole.roleId), onError: (err, userRole, context) => { console.group("onError") console.error('err', err) console.error('userRole', userRole) console.error('context', context) }, }) see the screenshot of console errors
No description
2 Replies
correct-apricot
correct-apricot3y ago
context is whatever you return from onMutate. It looks like you return nothing from onMutate, so its undefined
extended-salmon
extended-salmonOP3y ago
Thanks for the quick response ...I am returning the axios(BaseService) response ...do I need to to return something different export class UserQuery extends BaseQuery { path = 'users/' updateRole = async (userId :string, roleName:string) => { return await BaseService.post(this.url('updateRole'),{ uid : userId, roleId : roleName }) } } I think I understood ...I need to return previous value in on mutate

Did you find this page helpful?