toDos를 가져온 다음에
map 함수를 이용해서 하나씩 뽑아서 li에 넣어주기만 하면 됨!!
li 마다 index가 필요하기 때문에 li의 key로 index 하나 넣어주고
{toDos.map((toDo, index)=> <li key={index}>{toDo}</li>)}
결론적으로 전체 코드
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;
}
setToDo("")
setToDos((currentArray) => [toDo, ...currentArray])
}
console.log(toDos)
return (
<div>
<h1>My To Dos ({toDos.length})</h1>
<form onSubmit = {onSubmit}>
<input value = {toDo} onChange = {onChange} type = "text" placeholder = "Write your to do..." />
<button>Add To Do</button>
</form>
<hr />
{toDos.map((toDo, index)=> <li key={index}>{toDo}</li>)}
</div>
);
}
export default App;
'개발 > react' 카테고리의 다른 글
react로 api 호출해서 화면에 표시하기!! (0) | 2022.10.03 |
---|---|
react의 useEffect는 api call 할 때 많이 사용!! (0) | 2022.09.26 |
react array에서 [a, ...b]의 사용!! (0) | 2022.09.26 |
react에서 useState와 useEffect 사용 예시!! (0) | 2022.09.25 |
react의 useEffect는 딱 한번만 실행되며 dependency에서 변화되는 것을 지켜봄!! (0) | 2022.09.25 |