ėēė ėąė ëĪėīėė ë ëĄëĐ ëĐėė§ëĨž ëģīėŽėĢžęģ ė―ėļëĪėī ëėīëëĐī ëĄëĐ ëĐėė§ëĨž ėĻęļī í ė―ėļëĪė ė ëĶŽë ëĶŽėĪíļëĄ ëģīėŽėĢžëëĄ íīėž íëĪ.
import { useState, useEffect } from "react";
function App() {
const [loading, setLoading] = useState(true);
const [coins, setCoins] = useState([]);
// ėŧīíŽëíļę° ėēė ėėąëė ë fetchëĨž ėīėĐíī ėėēė ëģīëļ í ėëĩė ë°ėėĻëĪ
useEffect(() => {
fetch("https://api.coinpaprika.com/v1/tickers")
.then((response) => response.json())
.then((data) => {
setCoins(data);
setLoading(false); // ėīë loading ëģėė ę°ë ë°ęŋėž íëĪ
});
}, []);
return (
<div>
<h1>The Coins ({loading ? null : `(${coins.length})`)</h1>
{loading ? <strong>ëĄëĐ ėĪ...</strong> : (
<ul>
{coins.map((coin) => (
<li key={coin.id}>
{coin.name} ({coin.symbol}): ${coin.quotes.USD.price} USD
</li>
))}
</ul>
)}
</div>
);
}
export default App;
ėėė ë°°ėī ëīėĐ ëģĩėĩ
const [amount, setAmount] = useState(0);
const [flipped, setFlipped] = useState(false);
const onChange = (event) => setAmount(event.target.value);
const onFlip = () => {
setFlipped((prev) => !prev);
reset();
};
const reset = () => setAmount(0);
return (
<div>
// ėĪëĩ
<div>
<input
value={!flipped ? amount : amount * 36955}
onChange={onChange}
placeholder="USD"
type="number"
disabled={flipped}
/>
<input
value={flipped ? amount : amount * 0.000027}
onChange={onChange}
placeholder="BTC"
type="number"
disabled={!flipped}
/>
</div>
<button onClick={onFlip}>Flip</button>
<button onClick={reset}>Reset</button>
<hr />
// ėĪëĩ
</div>
);