Last updated on August 28, 2024 pm
钱币转换小app
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| import { useEffect, useState } from "react";
export default function App() { const [amount, setAmount] = useState(1); const [fromCur, setFromCur] = useState("EUR"); const [toCur, setToCur] = useState("USD"); const [converted, setConverted] = useState(""); const [isLoading, setIsLoading] = useState(false);
useEffect( function () { async function convert() { setIsLoading(true); const res = await fetch( `https://api.frankfurter.app/latest?amount=${amount}&from=${fromCur}&to=${toCur}` ); const data = await res.json(); setConverted(data.rates[toCur]); setIsLoading(false); } if (fromCur === toCur) return setConverted(amount); convert(); }, [amount, fromCur, toCur] ); return ( <div> <input type="text" value={amount} onChange={(e) => setAmount(Number(e.target.value))} disabled={isLoading} /> <select value={fromCur} onChange={(e) => setFromCur(e.target.value)} disabled={isLoading} > <option value="USD">USD</option> <option value="EUR">EUR</option> <option value="CAD">CAD</option> <option value="INR">INR</option> </select> <select value={toCur} onChange={(e) => setToCur(e.target.value)} disabled={isLoading} > <option value="USD">USD</option> <option value="EUR">EUR</option> <option value="CAD">CAD</option> <option value="INR">INR</option> </select> <p> {converted} {toCur} </p> </div> ); }
|
react课程-challenge
http://example.com/2024/08/28/react课程-challenge/