Skip to content

Commit

Permalink
fix: 수정을 받고 싶은 상태로 변경한다;
Browse files Browse the repository at this point in the history
  • Loading branch information
gmkim716 committed Jan 5, 2025
1 parent 0580826 commit c7e7130
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
12 changes: 6 additions & 6 deletions src/@lib/hooks/ver1/useItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useMemo, useState } from "react";
import { generateItems } from "../../../utils";
import { ITEMS_LENGTH } from "../../../const";

export const useHandleItem = () => {
export function useItem() {
const [items, setItems] = useState(() => generateItems(ITEMS_LENGTH));
const [filter, setFilter] = useState("");

Expand All @@ -18,19 +18,19 @@ export const useHandleItem = () => {
items.filter(
(item) =>
item.name.toLowerCase().includes(filter.toLowerCase()) ||
item.category.toLowerCase().includes(filter.toLowerCase()),
item.category.toLowerCase().includes(filter.toLowerCase())
),
[items, filter],
[items, filter]
);

const totalPrice = useMemo(
() => filteredItems.reduce((sum, item) => sum + item.price, 0),
[filteredItems],
[filteredItems]
);

const averagePrice = useMemo(
() => Math.round(totalPrice / filteredItems.length) || 0,
[totalPrice, filteredItems.length],
[totalPrice, filteredItems.length]
);

return {
Expand All @@ -41,4 +41,4 @@ export const useHandleItem = () => {
totalPrice,
averagePrice,
};
};
}
4 changes: 2 additions & 2 deletions src/@lib/hooks/ver2/useItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export function useItem() {
items.filter(
(item) =>
item.name.toLowerCase().includes(filter.toLowerCase()) ||
item.category.toLowerCase().includes(filter.toLowerCase()),
item.category.toLowerCase().includes(filter.toLowerCase())
),
[items, filter],
[items, filter]
);

return {
Expand Down
5 changes: 2 additions & 3 deletions src/@lib/hooks/ver2/usePrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { useItem } from "./useItem";

export function usePrice() {
const { filteredItems } = useItem();

const totalPrice = useMemo(
() => filteredItems.reduce((sum, item) => sum + item.price, 0),
[filteredItems],
[filteredItems]
);

const averagePrice = useMemo(
() => Math.round(totalPrice / filteredItems.length) || 0,
[totalPrice],
[totalPrice]
);

return { totalPrice, averagePrice };
Expand Down
42 changes: 23 additions & 19 deletions src/components/ItemList.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import { useCallback, useState } from "react";
import { generateItems, renderLog } from "../utils";
import { useState } from "react";
import { renderLog } from "../utils";
import { useTheme } from "../context/ThemeContext";
import { ITEMS_LENGTH } from "../const";
import { usePrice } from "../@lib/hooks/ver2/usePrice";
import { useItem } from "../@lib/hooks/ver2/useItem";

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

// Question. 훅으로 분리한다면?
const { filteredItems, addItems } = useItem();
const { totalPrice, averagePrice } = usePrice();

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

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

const filteredItems = items.filter(
(item) =>
item.name.toLowerCase().includes(filter.toLowerCase()) ||
item.category.toLowerCase().includes(filter.toLowerCase()),
);
// const [items, setItems] = useState(() => generateItems(ITEMS_LENGTH));
// const addItems = useCallback(() => {
// setItems((prevItems) => [
// ...prevItems,
// ...generateItems(ITEMS_LENGTH, prevItems.length),
// ]);
// }, []);

const totalPrice = filteredItems.reduce((sum, item) => sum + item.price, 0);
// const filteredItems = items.filter(
// (item) =>
// item.name.toLowerCase().includes(filter.toLowerCase()) ||
// item.category.toLowerCase().includes(filter.toLowerCase()),
// );

const averagePrice = Math.round(totalPrice / filteredItems.length) || 0;
// const totalPrice = filteredItems.reduce((sum, item) => sum + item.price, 0);
// const averagePrice = Math.round(totalPrice / filteredItems.length) || 0;

return (
<div className="mt-8">
Expand Down

0 comments on commit c7e7130

Please sign in to comment.