-
Notifications
You must be signed in to change notification settings - Fork 0
[Week2] 규서 2주차 미션_1 #21
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?
Conversation
| const ctx = useContext(TodoContext); | ||
| if (!ctx) throw new Error("useTodo must be used within <TodoProvider>"); | ||
| return ctx; | ||
| } |
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.
불필요한 리렌더링 방지: 현재 TodoProvider가 리렌더링될 때마다 add, complete, remove 함수가 매번 새로 생성됩니다. 이는 이 함수들을 props로 받는 하위 컴포넌트들의 불필요한 리렌더링을 유발할 수 있습니다.
useCallback 적용: useCallback 훅을 사용하여 이 함수들을 메모이제이션하면, input, todos 등 의존성 배열에 포함된 상태가 변경될 때만 함수를 재생성합니다. 이를 통해 앱의 렌더링 성능을 개선할 수 있습니다.
import { useState, useCallback } from "react";
// ...
const add = useCallback(() => {
// ...
}, [input, setTodos, setInput]);
const complete = useCallback((task: Task) => {
// ...
}, [todos, setTodos, setDoneTasks]);
// ...
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.
위에서 말씀해주신대로
첫번째로는 complete/remove가 Task 객체 대신 id를 받게 변경하여 객체 참조 문제 예방하였습니다.
두번째로는 useCallback으로 핸들러 고정, useMemo로 value 고정하여 리렌더 노이즈 감소시켰습니다.
존재하지 않는 id가 들어와도 안전하게 무시할 수 있도록 개선했습니다
위 세 가지 내요을 기반을 코드 보완 완료했습니다!
|
프로젝트 폴더 이름 Guser_01 로 변경해주세요 |
2주차 미션에서는 React에 대한 기초 개념을 배우며 전반적인 큰 틀을 알게 되었습니다.