1
export const useDeleteArticles = ({ ids, onSuccess }) => {
  const queryResult = useQueries(
    ids.map(id => ({
      queryKey: ["article-delete", id],
      queryFn: () => articlesApi.destroy(id),
    }))
  );

  const isLoading = queryResult.some(result => result.isLoading);

  if (!isLoading) {
    onSuccess();
  }

  return { isLoading, queryResult };
};

This customHook will simply delete some articles.

I tried to use enabled with a state as following.

export const useDeleteArticles = ({ ids, onSuccess, enabled }) => {
  const queryResult = useQueries(
    ids.map(id => ({
      queryKey: ["article-delete", id],
      queryFn: () => articlesApi.destroy(id),
      enabled,
    }))
  );

  const isLoading = queryResult.some(result => result.isLoading);

  if (!isLoading) {
    onSuccess();
  }

  return { isLoading, queryResult };
};
const [enabled, setEnabled] = useState(false);
useDeleteArticles({ ids, onSuccess: refetch, enabled });

enabled && setEnabled(false);    //to avoid api call after deleting the articles

const handleArticleDelete = () => {    //this function will invoke onClick button
    setEnabled(true);
};

But this not making the api call. could anyone help me to implement this in correct way.

Thank you.

2

1 Answer 1

0

This customHook will simply delete some articles.

a delete operation is almost never a query, but a mutation. Mutations can be fired imperatively by calling the mutate function returned from useMutation. A query is the wrong thing to use for this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.