UNPKG

marinafetchreacthook

Version:

custom hook

22 lines (18 loc) 603 B
import { useState, useEffect } from "react"; import axios from "axios"; export default function useApi(url, options) { const [isLoading, setLoading] = useState(false); const [isError, setError] = useState(null); const [data, setData] = useState([]); useEffect(() => { setLoading(true); axios({ url, ...options, }) .then((res) => setData(res.data)) .catch(() => setError(true)) .finally(() => setLoading(false)); }, [url, options]); return { isLoading, isError, data }; }