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.