-
Notifications
You must be signed in to change notification settings - Fork 4
[3주차] 장승윤/[feat] 주요 기능 추가 #31
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: 장승윤/main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uC7A5\uC2B9\uC724/3\uC8FC\uCC28"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,31 +14,28 @@ function App() { | |
| Completed: "completed" | ||
| } | ||
|
|
||
|
|
||
| const keyCount = useRef(3) | ||
| const [completedCount, setCompletedCount] = useState(0) | ||
| const nameText = useRef() | ||
| const [editingId, setEditingId] = useState(0) | ||
| const [tasks, setTasks] = useState([ | ||
| {id:1, isCompleted: false, taskName: "Eat"}, | ||
| {id:2, isCompleted: false, taskName: "Sleep"}, | ||
| {id:3, isCompleted: false, taskName: "Repeat"} | ||
| ]) | ||
| const activeCount = tasks.filter(task => !task.isCompleted).length; | ||
|
|
||
| /*------------------------------------------------------------------------------------------*/ | ||
|
|
||
| const addTask = ()=> { | ||
| keyCount.current++ | ||
| nameText.current.value.trim() && setTasks(prev => [...prev, {id: keyCount.current, isCompleted: false, taskName: nameText.current.value}]) | ||
| const newNameText = nameText.current.value | ||
| if (newNameText.trim()) { | ||
| nameText.current.value = "" | ||
| keyCount.current++ | ||
| setTasks(prev => [...prev, {id: keyCount.current, isCompleted: false, taskName: newNameText}]) | ||
|
Comment on lines
+32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이전의 코드는 한 줄에 많은 내용이 담겨 더 고민이 필요했다면 지금은 줄마다 다른 내용이 되어 더 직관적이라 보기도 좋고 다른 사람이 이해하기에도 더 쉬운 코드가 된 거 같습니다. |
||
| } | ||
| } | ||
|
|
||
| const deleteTask = (id) => { | ||
| tasks.map(task => { | ||
| if (task.id === id) { | ||
| task.isCompleted && setCompletedCount(prev => --prev) | ||
| } | ||
| }) | ||
|
|
||
| setTasks(prev => prev.filter(task => task.id !== id)) | ||
| } | ||
|
|
||
|
|
@@ -61,14 +58,10 @@ function App() { | |
| } | ||
| } | ||
|
|
||
| const checkHandler = (id, isChecked) => { | ||
| const copiedTasks = tasks.map(task => { | ||
| if (task.id === id) { | ||
| isChecked ? setCompletedCount(prev => ++prev) : setCompletedCount(prev => --prev) | ||
| return {...task, isCompleted: isChecked} | ||
| } else {return task} | ||
| }) | ||
|
|
||
| const countCompleted = (id, isChecked) => { | ||
| const copiedTasks = tasks.map(task => | ||
| task.id === id ? { ...task, isCompleted: isChecked } : task | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addTask와 deleteTask에서 prev을 사용하여 상태를 업데이트 한 부분이 좋았습니다. countCompleted도 다른 것과 마찬가지로 코드의 일관성을 위해 prev을 사용하는 것을 고려하면 좋을 것 같습니다! |
||
| setTasks(copiedTasks) | ||
| } | ||
|
|
||
|
|
@@ -89,15 +82,15 @@ function App() { | |
| <FliterButtons/> | ||
|
|
||
| {/* 남은 Task 수 표시 */} | ||
| <h2 className='w-94 mt-3.5 mb-2.5 font-bold'>{tasks.length - completedCount} tasks remaining </h2> | ||
| <h2 className='w-94 mt-3.5 mb-2.5 font-bold'>{activeCount} tasks remaining </h2> | ||
|
|
||
| {/* 필터별 리스트 */} | ||
| <Routes> | ||
| <Route path="/" element={<Navigate to="/all" replace/>} /> | ||
| <Route path="/all" element={ | ||
| <List | ||
| tasks={filterTasks(Filters.All)} | ||
| checkHandler={checkHandler} | ||
| checkHandler={countCompleted} | ||
| editTask={editTask} | ||
| deleteTask={deleteTask} | ||
| editingId={editingId} | ||
|
|
@@ -107,7 +100,7 @@ function App() { | |
| <Route path="/active" element={ | ||
| <List | ||
| tasks={filterTasks(Filters.Active)} | ||
| checkHandler={checkHandler} | ||
| checkHandler={countCompleted} | ||
| editTask={editTask} | ||
| deleteTask={deleteTask} | ||
| editingId={editingId} | ||
|
|
@@ -117,7 +110,7 @@ function App() { | |
| <Route path="/completed" element={ | ||
| <List | ||
| tasks={filterTasks(Filters.Completed)} | ||
| checkHandler={checkHandler} | ||
| checkHandler={countCompleted} | ||
| editTask={editTask} | ||
| deleteTask={deleteTask} | ||
| editingId={editingId} | ||
|
|
||
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.
editingId의 초기값이 현재 0으로 설정되어 있는데 편집 중인 항목이 없다는 의미라면 null을 사용하는 것이 더 적절할것같습니다. 0은 실제로 존재할 수 있는 값으로 오해될 여지가 있지만 null은 “선택된 항목이 없음”을 명확하게 표현합니다. 또한 코드 내부에서도 setEditingId(null)을 사용하고 있어 초기값을 null로 통일하면 상태의 의미를 더 일관되게 유지할 수 있습니다.