Jieunny의 블로그

Todo-list 본문

React/Nomadcoders

Todo-list

Jieunny 2021. 12. 2. 16:02
import { useEffect, 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;
    }
    setToDos(currentArray => [toDo, ...toDos]) //function(currentArray)와 같은 것
    setToDo("");
  };
  console.log(toDos);
  return (
    <div>
      <h1>My To Dos ({toDos.length})</h1>
      <form onSubmit={onSubmit}>
        <input 
          onChange={onChange} 
          value={toDo} type="text" 
          placeholder="Write your to do...">
        </input>
        <button>Add To Do</button>
        </form> 
        <hr />
        {toDos.map((toDo,index)=> <li key={index}>{toDo}</li>)} 
    </div>
  );
}
export default App;

'React > Nomadcoders' 카테고리의 다른 글

Movie App  (0) 2021.12.26
Coin Tracker  (0) 2021.12.26
Clean up function  (0) 2021.12.02
useEffect  (0) 2021.12.02
Props  (0) 2021.11.29