UNPKG

kawkab-frontend

Version:

Kawkab frontend is a frontend library for the Kawkab framework

23 lines (22 loc) 684 B
import { useState, useEffect, useCallback } from 'react'; export function useApi(apiCall) { const [state, setState] = useState({ data: null, loading: true, error: null, }); const fetchData = useCallback(async () => { setState({ data: null, loading: true, error: null }); try { const response = await apiCall(); setState({ data: response, loading: false, error: null }); } catch (err) { setState({ data: null, loading: false, error: err }); } }, [apiCall]); useEffect(() => { fetchData(); }, [fetchData]); return { ...state, refetch: fetchData }; }