UNPKG

@supunlakmal/hooks

Version:

A collection of reusable React hooks

20 lines 642 B
import { useState, useEffect, useCallback } from 'react'; export const usePromise = (promiseFn) => { const [data, setData] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const reload = useCallback(() => { setLoading(true); setError(null); setData(null); promiseFn() .then(setData) .catch(setError) .finally(() => setLoading(false)); }, [promiseFn]); useEffect(() => { reload(); }, [reload]); return { data, error, loading, reload }; }; //# sourceMappingURL=usePromise.js.map