반응형
import { useEffect, useState } from 'react';
import PropTypes from 'prop-types';

function ConinSelector(props) {
  return (
    <select onChange={props.onChange} style={{ width: '300px' }}>
      {props.coins.map((coin, index) => (
        <option key={index}>
          {coin.name} ({coin.symbol}) - {coin.quotes.USD.price}
        </option>
      ))}
    </select>
  );
}

ConinSelector.prototype = {
  coins: PropTypes.array.isRequired,
  onChange: PropTypes.func.isRequired,
};

function CoinTracker() {
  const [isLoading, setIsLoading] = useState(true);
  const [coins, setCoins] = useState([]);

  useEffect(() => {
    fetch('https://api.coinpaprika.com/v1/tickers')
      .then((response) => response.json())
      .then((json) => {
        setCoins(json);
        setIsLoading(false);
      });
  }, []);

  const onCoinSelectChanged = (event) => {
    console.log('onCoinSelectChanged : ' + event.target.value);
  };

  return (
    <div>
      <h1>Coin Tracker</h1>
      {isLoading ? (
        <strong>Loading...</strong>
      ) : (
        <ConinSelector coins={coins} onChange={onCoinSelectChanged} />
      )}
    </div>
  );
}

export default CoinTracker;

반응형

'[====== Development ======] > React' 카테고리의 다른 글

이미지 파일 선택하여 Preview 출력  (0) 2022.03.16
[React Study] Link Page  (0) 2022.01.16
Rounded Input textbox  (0) 2022.01.11
[React Study] Movie List  (0) 2022.01.11
[React Study] Simple Todos  (0) 2022.01.08

+ Recent posts