Skip to content

Commit

Permalink
refactor: 코드를 수정합니다
Browse files Browse the repository at this point in the history
  • Loading branch information
gmkim716 committed Jan 3, 2025
1 parent 0711ebb commit 62a639c
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npx lint-stagedx
npm run tsc && npm run prettier:write && npm run lint:fix
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ export default tseslint.config(
},
],
},
}
},
);
15 changes: 5 additions & 10 deletions src/@lib/equalities/shallowEquals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ export function shallowEquals<T extends object>(objA: T, objB: T): boolean {
return true;
}

// 2. 둘 중 하나라도 객체가 아닌 경우 처리
if (
typeof objA !== "object" ||
objA === null ||
typeof objB !== "object" ||
objB === null
) {
// 2. 타입 체크 - null도 함께 처리
// (typeof null === "object"이지만, null은 falsy value이므로
// 조건문에서 false로 평가됩니다)
if (!objA || !objB || typeof objA !== "object" || typeof objB !== "object") {
return false;
}

// 3. 객체의 키 개수가 다른 경우 처리
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
Expand All @@ -24,8 +20,7 @@ export function shallowEquals<T extends object>(objA: T, objB: T): boolean {

// 4. 모든 키에 대해 얕은 비교 수행
for (const key of keysA) {
// Q. hasOwn에 에러가 발생합니다. 어떻게 수정해야 좋을까요?
if (!Object.hasOwn(objB, key) || objA[key] !== objB[key]) {
if (objA[key as keyof T] !== objB[key as keyof T]) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/@lib/hooks/useCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { shallowEquals } from "../equalities";
export function useCallback<T extends Function>(
callback: T,
deps: DependencyList,
_equals = shallowEquals,
equals = shallowEquals
): T {
// useMemo를 사용하여 의존성이 변경될 때만 새로운 함수를 생성
return useMemo(() => callback, deps, _equals);
return useMemo(() => callback, deps, equals);
}
8 changes: 3 additions & 5 deletions src/components/ItemList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ export const ItemList: React.FC = () => {
const [filter, setFilter] = useState("");
const { theme } = useTheme();

// () => generateItems(1000): 왜 이렇게하면 괜찮을까?
// 화살표 함수를 했을 때는 첫 렌더링 시에만
// generateItems, 컴포넌트가 리렌더링 될때마다 호출하게 된다
// gus
// () => generateItems(1000): 화살표 함수를 했을 때는 첫 렌더링 시에만
// generateItems, 컴포넌트가 리렌더링될때마다 호출하게 된다
const [items, setItems] = useState(() => generateItems(1000));

const addItems = useCallback(() => {
Expand All @@ -24,7 +22,7 @@ export const ItemList: React.FC = () => {
const filteredItems = items.filter(
(item) =>
item.name.toLowerCase().includes(filter.toLowerCase()) ||
item.category.toLowerCase().includes(filter.toLowerCase())
item.category.toLowerCase().includes(filter.toLowerCase()),
);

const totalPrice = filteredItems.reduce((sum, item) => sum + item.price, 0);
Expand Down
34 changes: 0 additions & 34 deletions src/context/ItemContext.tsx

This file was deleted.

6 changes: 3 additions & 3 deletions src/context/NotificationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const NotificationContextProvider = ({

const removeNotification = useCallback((id: number) => {
setNotifications((prev) =>
prev.filter((notification) => notification.id !== id)
prev.filter((notification) => notification.id !== id),
);
}, []);

Expand All @@ -43,7 +43,7 @@ export const NotificationContextProvider = ({
addNotification,
removeNotification,
}),
[notifications, addNotification, removeNotification]
[notifications, addNotification, removeNotification],
);

return (
Expand All @@ -58,7 +58,7 @@ export const useNotification = () => {
const context = useContext(NotificationContext);
if (!context)
throw new Error(
"useNotification must be used within a NotificationProvider"
"useNotification must be used within a NotificationProvider",
);
return context;
};
2 changes: 1 addition & 1 deletion src/context/ThemeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const ThemeContextProvider = ({ children }: PropsWithChildren) => {
theme,
toggleTheme,
}),
[theme, toggleTheme]
[theme, toggleTheme],
);

return (
Expand Down
2 changes: 1 addition & 1 deletion src/context/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const UserContextProvider = ({
setUser({ id: 1, name, email });
addNotification("성공적으로 로그인되었습니다", "success");
},
[addNotification]
[addNotification],
);

const logout = useCallback(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import App from "./App";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<App />
</StrictMode>
</StrictMode>,
);

0 comments on commit 62a639c

Please sign in to comment.