-
Notifications
You must be signed in to change notification settings - Fork 126
[코드리뷰 요청] 과제2 완료 #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: ctaaag
Are you sure you want to change the base?
[코드리뷰 요청] 과제2 완료 #170
Changes from 5 commits
493446f
db4b5f3
515cc00
2e8abc6
a4dcfe4
0fd3dea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import React, { useState } from 'react'; | ||
| import { nanoid } from 'nanoid'; | ||
| import Header from './components/Header'; | ||
| import TodoForm from './components/TodoForm'; | ||
| import TodoLists from './components/TodoLists'; | ||
|
|
||
| export default function App() { | ||
| const [newTodo, setNewTodo] = useState(''); | ||
| const [todos, setTodos] = useState([]); | ||
|
|
||
| const handleSubmit = (e) => { | ||
| e.preventDefault(); | ||
| setTodos([...todos, { id: nanoid(6), text: newTodo }]); | ||
| setNewTodo(''); | ||
| }; | ||
|
|
||
| const handleChange = (e) => { | ||
| setNewTodo(e.target.value); | ||
| }; | ||
|
|
||
| const handleClickComplete = (clickedItemID) => { | ||
| setTodos(todos.filter((item) => item.id !== clickedItemID)); | ||
| }; | ||
|
|
||
| return ( | ||
| <div> | ||
| <Header /> | ||
| <TodoForm | ||
| newTodo={newTodo} | ||
| onSubmit={handleSubmit} | ||
| onChange={handleChange} | ||
| /> | ||
| <TodoLists | ||
| hasTodos={todos.length} | ||
| todos={todos} | ||
| onClickComplete={handleClickComplete} | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import React from 'react'; | ||
|
|
||
| export default function Header() { | ||
| return <div>TO-DO</div>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import React from 'react'; | ||
|
|
||
| export default function NoneTodos() { | ||
| return <span>할 일이 없어요!</span>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import React from 'react'; | ||
|
|
||
| export default function ShowTodos({ todos, onClickComplete }) { | ||
| return ( | ||
| <ol> | ||
| {todos.map(({ id, text }) => ( | ||
| <li key={id}> | ||
| <span>{text}</span> | ||
| <button type="button" id={id} onClick={() => onClickComplete(id)}> | ||
| 완료 | ||
| </button> | ||
| </li> | ||
| ))} | ||
| </ol> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import React from 'react'; | ||
|
|
||
| export default function TodoForm({ newTodo, onSubmit, onChange }) { | ||
| return ( | ||
| <form onSubmit={onSubmit}> | ||
| <input type="text" value={newTodo} onChange={onChange} /> | ||
| <button type="submit">추가</button> | ||
| </form> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,15 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import NoneTodos from './NoneTodos'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import ShowTodos from './ShowTodos'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| export default function TodoLists({ hasTodos, todos, onClickComplete }) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| <> | ||||||||||||||||||||||||||||||||||||||||||||||
| {hasTodos ? ( | ||||||||||||||||||||||||||||||||||||||||||||||
| <ShowTodos todos={todos} onClickComplete={onClickComplete} /> | ||||||||||||||||||||||||||||||||||||||||||||||
| ) : ( | ||||||||||||||||||||||||||||||||||||||||||||||
| <NoneTodos /> | ||||||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||||||
| </> | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| export default function TodoLists({ hasTodos, todos, onClickComplete }) { | |
| return ( | |
| <> | |
| {hasTodos ? ( | |
| <ShowTodos todos={todos} onClickComplete={onClickComplete} /> | |
| ) : ( | |
| <NoneTodos /> | |
| )} | |
| </> | |
| ); | |
| } | |
| const isEmpty = (arr = []) => arr.length === 0; | |
| export default function TodoLists({ hasTodos, todos, onClickComplete }) { | |
| if (isEmpty(todos)) { | |
| return <NoneTodos />; | |
| } | |
| return ( | |
| <ShowTodos todos={todos} onClickComplete={onClickComplete} /> | |
| ); | |
| } |
See also
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
헉 코드 정말 깔끔해졌네요!
저는 hasTodos라는 이름으로 todo의 유무도 컴포넌트 인자로 넘겨주면서 너무 투머치 한 느낌도 있었거든요
isEmpty라는 함수로 빼서 저렇게 사용하는 것도 좋네요!
그리고 처음에 짤 때 가드클래스를 사용했었는데 윤석님이 짜신 것처럼 바로 export default에 넣을 생각을 못했었는데.. 익숙치 않아서 그랬던 것 같습니다!
보내주신 코드에서 지향한 부분 앞으로 코드에도 잘 녹여서 진행해볼게요! 감사해요 :)
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom'; | ||
| import App from './App'; | ||
|
|
||
| ReactDOM.render(<App />, document.getElementById('app')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
새롭게 투두로 등록할거라서 newTodo인 것 같아요. 나중에 수정 기능이 생기면 이 값으로 똑같이 사용할 것 같은데 그러면 이 변수의 이름은 newOrUpdateTodo가 되어야만 할거예요.