TanStackT
TanStack3y ago
6 replies
technological-jade

Access Mutation Data

Hello Guys,

What is the best way to access data returned by a mutation?
const mutation = useMutation({ mutationFn: useCreateInvitation });

export const useCreateInvitation = async (values: CreateInvitationFormData) => {
    const { token, ...data } = values
    return await axios.post(BACKEND_URL + '/project/invite', data, {
        headers: {
            'Content-Type': 'application/json',
            Accept: 'application/json',
            Authorization: "Bearer " + token
        },
    });
};


This functions returns the new created invitation
const values = {
      email,
      role,
      projectId,
      token,
    };

    mutation.mutateAsync(values, {
      onError: () => {
        toast.error('Something went wrong!');
        return null;
      },
    });

I tried mutation.data.data but it doesnt always works. Also i dont know if its better to access in the onSuccess Function or just after the block.

Does anyone know a reliable way to access this returned data.

Thanks in advance
Was this page helpful?