Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Issue in Currency Project #178

Open
rajatchauhan2 opened this issue Aug 21, 2024 · 1 comment
Open

API Issue in Currency Project #178

rajatchauhan2 opened this issue Aug 21, 2024 · 1 comment

Comments

@rajatchauhan2
Copy link

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/[email protected]/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
Copy link

great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants