zstoreq
Version:
zustand-query is a library that helps you to query You can cal your api and get the data and store it in zustand store
26 lines (25 loc) • 740 B
JavaScript
import { useState } from "react";
import { handleError } from "./errorHandler";
export function useMutationZustand({ mutationFn, onSuccess, onError, }) {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const mutate = async (variables) => {
setIsLoading(true);
setError(null);
try {
const result = await mutationFn(variables);
onSuccess?.(result);
return result;
}
catch (err) {
handleError(err);
setError(err);
onError?.(err);
throw err;
}
finally {
setIsLoading(false);
}
};
return { mutate, isLoading, error };
}