Files
Account/src/hooks/useApi.ts
Koosha Lahouti 6cb742ff39 fix: styles
2025-08-12 02:51:24 +03:30

27 lines
636 B
TypeScript

import { useState, useEffect } from 'react';
type ApiFunction<T> = () => Promise<{ data: T }>;
export function useApi<T>(apiFunction: ApiFunction<T>) {
const [data, setData] = useState<T | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<unknown>(null);
useEffect(() => {
const fetchData = async () => {
try {
const response = await apiFunction();
setData(response.data);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
};
fetchData();
}, [apiFunction]);
return { data, loading, error };
}