skip to Main Content

I’m trying to create a delete function wherein It will show a loading state using useMutate of tanstackquery, but the problem is when I delete the button, it doesn’t show the loading state even though I change the network speed.

enter image description here

const ButtonAction = ({ id }: ButtonActionProps) => {
  const router = useRouter();

  const { mutate: deletePost, isLoading } = useMutation({
    mutationFn: () => {
      return axios.delete(`/api/posts/${id}`);
    },
    onError: (error) => {
      console.error(error);
    },

    onSuccess: () => {
      router.push("/");
      router.refresh();
    },
  });

  

  return (
    <div>
      <Link href="/edit/1" className="btn mr-2">
        {" "}
        <Pencil /> Edit
      </Link>
      <button onClick={() => deletePost()} className="btn btn-error">
        {" "}
        {isLoading && (
          <span className="loading loading-spinner loaddeing-xs"></span>
        )}
        {isLoading ? (
          "Loading..."
        ) : (
          <>
            <Trash />
            Delete
          </>
        )}
      </button>
    </div>
  );
};

export default ButtonAction;

2

Answers


  1. it is easy to use isPending as an alternative or isLoading

    Login or Signup to reply.
  2. I am using tanstack v "5.8.1", and isLoading is not an available property for this version. try using isPending instead.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search