[TIL] 220316

Lee Syong·2022년 3ė›” 16ėž
0

TIL

ëŠĐ록 ëģīęļ°
198/204
post-thumbnail

📝 ė˜Ī늘 한 ęēƒ

  1. react 강ė˜ ëģĩėŠĩ

📚 ë°°ėšī ęēƒ (ëģĩėŠĩ)

useState / props / memo / propTypes / useEffect

1. React Hook ė—°ėŠĩ

1) ë‹Ļėœ„ ëģ€í™˜ęļ° 만ë“Īęļ°

inputė˜ valueė— ëģ€ėˆ˜ëĨž 할ë‹đ하ęģ  onChange í•Ļėˆ˜ė— ė˜í•ī 감ė§€ëœ í•īë‹đ inputė˜ event.target.valueė˜ 값ėī inputė˜ value가 되도록 하ė—Ž ę·ļ 값ėī ëģ€í™”í•  때마ë‹Ī ėŧī폮넌íŠļ가 ëĶŽë Œë”링 되ė–ī 화ëĐīėī ë‹Īė‹œ ę·ļë Īė§€ë„록 한ë‹Ī.

2) todo list 만ë“Īęļ°

import { useState } from "react";

function App() {
  const [todo, setTodo] = useState("");
  const [todos, setTodos] = useState([]);
  
  const onChange = (event) => setTodo(event.target.value);
  const onSubmit = (event) => {
    event.preventDefault();
    
    if (todo === "") {
      return;
    }
    
    setTodo(""); // ėīęąļ ëĻžė € ėž‘ė„ąí•ī도
    setTodos(currentArray => [todo, ...currentArray]); // ė—Žęļ°ė„œ todo가 ""는 ė•„니ë‹Ī. (âˆĩ ė•„ė§ ëĶŽë Œë”링 x)
  };
  
  return (
    <div>
      <form onSubmit={onSubmit}>
        <input onChange={onChange} value={todo} />
        <button>ėķ”ę°€</button>
      </form>
      <ul>
        {todos.map((todo, index) => <li key={index}>{todo}</li>)}
      </ul>
    </div>
  );
}

export default App;

3) coin tracker 만ë“Īęļ°

( ė―”ë“œ ėąŒëĶ°ė§€ )

ė „ ė„ļęģ„ ė•”í˜ļ 화폐 ėĒ…ëĨ˜ëĨž ëŠĻ두 가ė ļė˜Ļë‹Ī.
화폐ëĨž ė„ íƒí•œ 후 ė•Ąėˆ˜ëĨž ėž…ë Ĩ하ëĐī USD coin 가ėđ˜ëĨž ëģīė—ŽėĪ€ë‹Ī.

// App.js
import { useState, useEffect } from "react";
import Input from "./Input";

function App() {
  const [loading, setLoading] = useState(true);
  const [coins, setCoins] = useState({});
  const [amount, setAmount] = useState(0);
  const [selectValue, setSelectValue] = useState("select");
  const [disabled, setDisabled] = useState(true);

  const onInputValueChange = (event) => setAmount(event.target.value);
  const onSelectValueChange = (event) => {
    setSelectValue(event.target.value);
    event.target.value === "select" ? setDisabled(true) : setDisabled(false);
    setAmount(0);
  };

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

  return (
    <div>
      <h1>Coin Traker {loading ? "" : `(${coins.length})`}</h1>
      {loading ? (
        <h3>로ë”Đ ėĪ‘ėž…니ë‹Ī...</h3>
      ) : (
        <select onChange={onSelectValueChange} value={selectValue}>
          <option value="select">Select the currency</option>
          {coins.map((coin) => (
            <option
              value={coin.quotes.USD.price}
              key={coin.id}
            >{`${coin.name}(${coin.symbol})`}</option>
          ))}
        </select>
      )}
      <br />
      <Input onChange={onInputValueChange} value={amount} disabled={disabled} />
      <br />
      <Input
        value={selectValue === "select" ? 0 : amount * selectValue}
        disabled={disabled}
        readOnly={true}
      />
    </div>
  );
}

export default App;
// Input.js
import PropTypes from "prop-types";

function Input({ onChange, value, disabled, readOnly }) {
  return (
    <input
      onChange={onChange}
      value={value}
      disabled={disabled}
      readOnly={readOnly}
    ></input>
  );
}

Input.propTypes = {
  onChange: PropTypes.func,
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
  disabled: PropTypes.bool.isRequired,
  readOnly: PropTypes.bool,
};

export default Input;

âœĻ ë‚īėž 할 ęēƒ

  1. movie app 만ë“Īęļ°
profile
ëŠĨ동ė ėœžëĄœ ė‚īėž, 행ëģĩ하ęēŒðŸ˜

0개ė˜ 댓ęļ€