Skip to content

Commit

Permalink
fix: ItemContext를 제거합니다
Browse files Browse the repository at this point in the history
ItemContext을 지정했을 때 리렌더링 되는 영역에 대한 Context 정의할 이유가 부족합니다. 따라서 기존의 context를 제거합니다
  • Loading branch information
gmkim716 committed Jan 2, 2025
1 parent b3664f6 commit 0711ebb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 90 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx lint-stagedx
6 changes: 1 addition & 5 deletions src/@lib/equalities/deepEquals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ export function deepEquals<T>(objA: T, objB: T): boolean {

// 5. 모든 키에 대해 깊은 비교 수행
for (const key of keysA) {
if (
// Q. hasOwn에 에러가 발생합니다 어떻게 수정해야 좋을까요
!Object.hasOwn(objB as object, key) ||
!deepEquals((objA as any)[key], (objB as any)[key])
) {
if (!deepEquals(objA[key as keyof T], objB[key as keyof T])) {
return false;
}
}
Expand Down
38 changes: 14 additions & 24 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Header } from "./components/Header";
import { ItemList } from "./components/ItemList";
import { ComplexForm } from "./components/ComplexForm";
import { ThemeContextProvider } from "./context/ThemeContext";
import { ItemContextProvider, useItems } from "./context/ItemContext";
import { UserContextProvider } from "./context/UserContext";
import { NotificationContextProvider } from "./context/NotificationContext";
import { NotificationSystem } from "./components/NotificationSystem";
Expand All @@ -12,33 +11,24 @@ const App = () => {
<ThemeContextProvider>
<NotificationContextProvider>
<UserContextProvider>
<ItemContextProvider>
<Header />
<MainContent />
<NotificationSystem />
</ItemContextProvider>
{/* <ItemContextProvider> */}
<Header />
<div className="container mx-auto px-4 py-8">
<div className="flex flex-col md:flex-row">
<div className="w-full md:w-1/2 md:pr-4">
<ItemList />
</div>
<div className="w-full md:w-1/2 md:pl-4">
<ComplexForm />
</div>
</div>
</div>
<NotificationSystem />
{/* </ItemContextProvider> */}
</UserContextProvider>
</NotificationContextProvider>
</ThemeContextProvider>
);
};

// 아이템 리스트와 폼을 포함하는 메인 컨텐츠
const MainContent = () => {
const { items, addItems } = useItems();

return (
<div className="container mx-auto px-4 py-8">
<div className="flex flex-col md:flex-row">
<div className="w-full md:w-1/2 md:pr-4">
<ItemList items={items} onAddItemsClick={addItems} />
</div>
<div className="w-full md:w-1/2 md:pl-4">
<ComplexForm />
</div>
</div>
</div>
);
};

export default App;
26 changes: 18 additions & 8 deletions src/components/ItemList.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { useState } from "react";
import { Item } from "../types";
import { renderLog } from "../utils";
import { useCallback, useState } from "react";
// import { Item } from "../types";
import { generateItems, renderLog } from "../utils";
import { useTheme } from "../context/ThemeContext";

export const ItemList: React.FC<{
items: Item[];
onAddItemsClick: () => void;
}> = ({ items, onAddItemsClick }) => {
export const ItemList: React.FC = () => {
renderLog("ItemList rendered");
const [filter, setFilter] = useState("");
const { theme } = useTheme();

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

const addItems = useCallback(() => {
setItems((prevItems) => [
...prevItems,
...generateItems(1000, prevItems.length),
]);
}, []);

const filteredItems = items.filter(
(item) =>
item.name.toLowerCase().includes(filter.toLowerCase()) ||
Expand All @@ -29,7 +39,7 @@ export const ItemList: React.FC<{
<button
type="button"
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded text-xs"
onClick={onAddItemsClick}
onClick={addItems}
>
대량추가
</button>
Expand Down
36 changes: 0 additions & 36 deletions src/context/AppContext.tsx

This file was deleted.

17 changes: 0 additions & 17 deletions src/context/ItemContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,6 @@ const ItemContext = createContext<ItemContextType | null>(null);

// 3. provider
export const ItemContextProvider = ({ children }: PropsWithChildren) => {
const [items, setItems] = useState(generateItems(1000));

const addItems = useCallback(() => {
setItems((prevItems) => [
...prevItems,
...generateItems(1000, prevItems.length),
]);
}, []);

const value = useMemo(
() => ({
items,
addItems,
}),
[items, addItems]
);

return <ItemContext.Provider value={value}>{children}</ItemContext.Provider>;
};

Expand Down

0 comments on commit 0711ebb

Please sign in to comment.