
๐ก๋ ธ๋ง๋์ฝ๋ ๋๊ผฌ์ค์ ๊ฐ์๋ฅผ ๋ณด๋ฉฐ ๊ณต๋ถํ๋ ์๋ฆฌ์ฆ์ ๋๋ค.
์ํธํํ๋ค๊ณผ ๊ทธ ๊ฐ๊ฒฉ์ List ํ์์ผ๋ก ๋ณด์ฌ์ฃผ๋ ๊ฒ์ ๋ชฉํ๋ก ํ๋ ํ๋ก์ ํธ.
๋ชฉํ์๋ฆฝ
1. ํ์ด์ง or ์ฑ์ ์ ์ ํ์ ๋ loading ๋ฉ์ธ์ง๋ฅผ ๋ณด์ฌ์ค๋ค.
2. ์ฝ์ธ์ data๋ฅผ ๊ฐ์ ธ์ค๋ฉด loading ๋ฉ์ธ์ง๋ ์จ๊ธฐ๊ณ SUPER COOL COIN LIST๋ฅผ ๋ณด์ฌ์ค๋ค.
๐๐ป์ด๋ฒ์ ์ฌ์ฉ๋ API:coinpaprika
JSONView(์ต์คํ ์ ๋ฐ๋ก๊ฐ๊ธฐ)
ํด๋น APIํ์ด์ง๋ jsonํ์ผ์ rowํ์์ผ๋ก ๋ณด์ฌ์ฃผ๊ธฐ ๋๋ฌธ์ ์ธ๊ฐ์ด data๋ฅผ ์ฝ๊ธฐ์ ๊ต์ฅํ ๋ถํธํ๋ค. chrome์ ์ด์ฉํด์ ๊ฐ๋ฐ์ ํ๋ค๋ฉด google์ ํ์ฅํ๋ก๊ทธ๋จ์ ์ฌ์ฉํ๋ฉด data ์ฝ๊ธฐ๊ฐ ์์ํด์ง๋ค.
import { useState, useEffect } from 'react';
function App() {
//loading์ ์ด๊ธฐ๊ฐ์ false๋ก ์ธํ
const [loading, setLoading] = useState(true);
const [coins, setCoins] = useState([]);
useEffect(() => {
//fetch๋ก api์ data ๊ฐ์ ธ์จ ๋ค์์
fetch('https://api.coinpaprika.com/v1/tickers')
.then(
//res๋ฅผ res.jsonํ์์ ๋ฐฐ์ด๋ก ๋ฆฌํดํด๋ผ
(res) => res.json()
)
//๊ทธ ๋ค์์ json์ coins์ ๋ด์๋ผ.
.then((json) => setCoins(json));
//setLoading์ผ๋ก loading์ ๊ฐ์ false๋ก ๋ณ๊ฒฝ
setLoading(false);
}, []);
return (
<div>
<h1>The Coins({coins.length})</h1>
{loading ? (
<strong>Loading...</strong>
) : (
<ul>
{coins.map((item) => (
<li key={item.id}>
{item.name}({item.symbol}): $
{item.quotes.USD.price}
</li>
))}
</ul>
)}
</div>
);
}
export default App;
๊ฒฐ๊ณผ

์์ฉ : SELECT๋ก ๋ง๋ค๊ธฐ
import { useState, useEffect } from 'react';
function App() {
const [loading, setLoading] = useState(true);
const [coins, setCoins] = useState([]);
useEffect(() => {
fetch('https://api.coinpaprika.com/v1/tickers')
.then(
(res) => res.json()
)
.then((json) => setCoins(json));
setLoading(false);
}, []);
return (
<div>
<h1>The Coins({coins.length})</h1>
{loading ? (
<strong>Loading...</strong>
) : (
<select>
{coins.map((item) => (
<option key={item.id}>
{item.name}({item.symbol}): $
{item.quotes.USD.price}
</option>
))}
</select>
)}
</div>
);
}
export default App;

๋ชฉํ์๋ฆฝ
1. input์ ๊ธ์ก์ ์
๋ ฅํ๊ณ ,
2. select๋ก ์ฝ์ธ์ ์ ํํ๋ฉด
3. ๊ตฌ์
ํ ์ ์๋ ์ฝ์ธ์ ๊ฐฏ์๋ฅผ ์ถ๋ ฅํ๋ค.
import { useState, useEffect } from 'react';
function App() {
const [budget, setBudget] = useState(0);
const [count, setCount] = useState(0);
const [coins, setCoins] = useState([]);
function onChange(e) {
setBudget(e.target.value);
let exchangeRate = Math.round(budget / 1300);
setCount(exchangeRate / e.target.value);
}
function selectCoin(e) {
let coinPrice = Number(e.target.value.split('$')[1]);
let exchangeRate = Math.round(budget / 1300);
setCount(exchangeRate / coinPrice);
}
useEffect(() => {
fetch('https://api.coinpaprika.com/v1/tickers')
.then((res) => res.json())
.then((json) => setCoins(json));
}, []);
return (
<div>
<h1>The Coins({coins.length})</h1>
<div>
<label htmlFor="price">์์ฐ</label>
<input
type="number"
id="price"
value={budget}
onChange={onChange}
/>
</div>
<select onChange={selectCoin}>
{coins.map((item) => (
<option key={item.id}>
{item.name}({item.symbol}) : $
{item.quotes.USD.price}
</option>
))}
</select>
<div>
<label htmlFor="count">๊ฐฏ์</label>
<input
type="number"
id="count"
value={count.toFixed(5)}
disabled
/>
</div>
</div>
);
}
export default App;
๊ฒฐ๊ณผ

์๋ฆฝํ ๋ชฉํ๋๋ก๋ ์ ๋์ค์ง๋ง ๋ถ์์ฐ์ค๋ฝ๋ค.
ํ์ฌ ์
๋ ํธ ๋์ด์๋ ์ต์
์ ๊ฐ๊ฒฉ์ ๋ฐ์์ค useState๋ฅผ ๋ง๋ค๊ณ , select์์ defaultValue๋ก coins๊ฐ ๋น๊ฐ์ด ์๋๋ผ๋ฉด coins[0]์์ price๋ฅผ state๋ก ๋๊ธฐ๋ผ๊ณ ํ๋ค.
coins๊ฐ null์ด ์๋๊ธฐ ๋๋ฌธ์ coins[0]์ ์์๋ค์ ๋ฐ์ ์ฌ ์ ์์ ์ค ์์๋๋ฐ ์ฐพ์ ์ ์๋ค๊ณ ํ๋ค...
๋ค์ ์ง๋๋ฅผ ๋๊ฐ์ผ ํ๋ฏ๋ก ์ด๋ฒ ๋ฌธ์ ๋ ๊ณต๋ถํ๋ฉด์ ํด๊ฒฐํด ๋ณด๋ ๊ฒ์ผ๋ก ๋ง๋ฌด๋ฆฌ...๐ข
๋ฆฌ์ํธ์์ ์
๋ ํธ ๋ง๋ค๋ ์์ฃผ ์ด๋ค๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ๊ฒฌ!
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํด์ ์ข ๋ ๋ฐ์ ์์ผ๋ณผ ์ ์์ ๊ฒ ๊ฐ๋ค.
๐๐ป๋๋ฐ์๋๋ฑ๋์ ๋ธ๋ก๊ทธ
๐๐ปreact-select