hiteshchoudhary / chai-aur-react

chai aur react series on youtube
7.92k stars 1.02k forks source link

API Issue in Currency Project #178

Open rajatchauhan2 opened 1 month ago

rajatchauhan2 commented 1 month ago

import { useEffect, useState } from "react";

function useCurrencyInfo(currency) { const [data, setData] = useState({}); const [error, setError] = useState(null);

useEffect(() => {
    const fetchData = async () => {
        try {
            const response = await fetch(`https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@2024.8.21/v1/currencies/${currency}.json`);
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            const result = await response.json();
            setData(result[currency]);
        } catch (err) {
            setError(err.message);
            console.error("Fetch error: ", err);
        }
    };

    fetchData();
}, [currency]);

if (error) {
    return { error };
}

return data;

}

export default useCurrencyInfo;

I'm doing this way to solve

Nomeena22 commented 1 month ago

great