Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown

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로 통일하면 상태의 의미를 더 일관되게 유지할 수 있습니다.

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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))
}

Expand All @@ -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
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addTask와 deleteTask에서 prev을 사용하여 상태를 업데이트 한 부분이 좋았습니다. countCompleted도 다른 것과 마찬가지로 코드의 일관성을 위해 prev을 사용하는 것을 고려하면 좋을 것 같습니다!

setTasks(copiedTasks)
}

Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand Down
3 changes: 1 addition & 2 deletions src/components/FilterButtons.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import clsx from "clsx"
import { NavLink } from 'react-router'
export const FliterButtons = ({onClick, children, className}) => {
export const FliterButtons = () => {
return <div>
<NavLink to="/all" className={({ isActive }) => clsx(
'w-30 inline-block h-8 text-center leading-8 rounded', isActive ? 'bg-black text-white' : 'outline outline-gray-300',
Expand Down