TanStackT
TanStackโ€ข2y agoโ€ข
4 replies
ripe-gray

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 };
}
Was this page helpful?