T
TanStack•2y ago
harsh-harlequin

Mutate send response

Not sure if im being stupid or what, is the best way to pass back the response using useState or is there a easier way for me to access the response. Also any advice to clean up the code is appreciated šŸ™‚
import { useMutation } from '@tanstack/react-query';
import { Endpoints } from '@/config';
import { client } from '@/config';
import { useState } from 'react';

export interface Unloadable {
selectedStillageID: string;
selectedEventID: string;
}

async function checkStillageIsUnloadable(unloadable: Unloadable) {
const response = await client.post({
url: Endpoints.Stillage.StillageIsUnloadable,
body: unloadable,
});
return response.data;
}

export function useCheckStillageIsUnloadable() {
const [response, setResponse] = useState<any>();

const { mutate, isSuccess, isLoading, isIdle, isError } = useMutation({
mutationKey: ['checkStillageIsUnloadable'],
mutationFn: checkStillageIsUnloadable,
onSuccess: (res) => {
setResponse(res);
},
});

return { mutate, response, isSuccess, isLoading, isIdle, isError };
}
import { useMutation } from '@tanstack/react-query';
import { Endpoints } from '@/config';
import { client } from '@/config';
import { useState } from 'react';

export interface Unloadable {
selectedStillageID: string;
selectedEventID: string;
}

async function checkStillageIsUnloadable(unloadable: Unloadable) {
const response = await client.post({
url: Endpoints.Stillage.StillageIsUnloadable,
body: unloadable,
});
return response.data;
}

export function useCheckStillageIsUnloadable() {
const [response, setResponse] = useState<any>();

const { mutate, isSuccess, isLoading, isIdle, isError } = useMutation({
mutationKey: ['checkStillageIsUnloadable'],
mutationFn: checkStillageIsUnloadable,
onSuccess: (res) => {
setResponse(res);
},
});

return { mutate, response, isSuccess, isLoading, isIdle, isError };
}
2 Replies
correct-apricot
correct-apricot•2y ago
it's available in data returned from useMutation šŸ˜…
harsh-harlequin
harsh-harlequinOP•2y ago
Wait what am I really that dumb, i didnt even realise there was a data... šŸ¤¦ā€ā™‚ļø Wow im sorry for even asking that questions, thanks though!

Did you find this page helpful?