260202_03_ENH: 페이지 깜빡임 최소화 및 카테고리에서 전체로 이동 시 페이지 렌더링 2회 되는 부분 수정
This commit is contained in:
@@ -67,6 +67,8 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
const [totalValue, setTotalValue] = useState<SelectedMap>(() => buildDefaultTotalValue());
|
||||
const [categoryValue, setCategoryValue] = useState<CategoryFilterValue>(() => buildDefaultCategoryValue(categoryFilterItems));
|
||||
|
||||
const prevIsTotalRef = useRef<boolean>(isTotal);
|
||||
|
||||
const movePage = (path: string) => {
|
||||
router.push(path);
|
||||
};
|
||||
@@ -86,30 +88,38 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
scrollRef.current?.scrollTo({ top: 0, behavior: "smooth" });
|
||||
};
|
||||
|
||||
const ctx = useMemo<ExcelExportContext>(() => {
|
||||
return isTotal
|
||||
? { isTotal: true, filterValue: totalValue }
|
||||
: { isTotal: false, filterValue: categoryValue };
|
||||
}, [isTotal, totalValue, categoryValue]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isTotal){
|
||||
console.log("istotal category", buildDefaultCategoryValue(PRODUCTS_FILTER_ITEM[0]));
|
||||
setCategoryValue(buildDefaultCategoryValue(PRODUCTS_FILTER_ITEM[0]))
|
||||
const prevIsTotal = prevIsTotalRef.current;
|
||||
|
||||
// 1) category -> total : 아무것도 안 함 (여기서 setCategoryValue 하면 1번 더 렌더됨)
|
||||
if (isTotal) {
|
||||
prevIsTotalRef.current = isTotal;
|
||||
return;
|
||||
}
|
||||
|
||||
if(categoryValue.product != categoryFilterItems.items[0]){
|
||||
setCategoryValue((prev) => {
|
||||
return { ...prev, product: categoryFilterItems.items[0] };
|
||||
});
|
||||
// 2) total -> category : 들어오는 순간에만 초기화
|
||||
if (prevIsTotal) {
|
||||
setCategoryValue(buildDefaultCategoryValue(categoryFilterItems));
|
||||
prevIsTotalRef.current = isTotal;
|
||||
return;
|
||||
}
|
||||
|
||||
// 3) category 내부 이동(카테고리 path 바뀜 등) : product만 동기화
|
||||
const nextProduct = categoryFilterItems.items[0];
|
||||
setCategoryValue((prev) => {
|
||||
if (prev.product === nextProduct) return prev;
|
||||
return { ...prev, product: nextProduct };
|
||||
});
|
||||
|
||||
prevIsTotalRef.current = isTotal;
|
||||
}, [isTotal, categoryFilterItems]);
|
||||
|
||||
return (
|
||||
<div className="h-screen flex flex-col bg-transparent overflow-hidden">
|
||||
<LayoutFilterProvider value={ctx} >
|
||||
<LayoutFilterProvider
|
||||
value={isTotal? { isTotal: true, filterValue: totalValue } : { isTotal: false, filterValue: categoryValue }}
|
||||
>
|
||||
<Header
|
||||
activeTopId={activeTop.id}
|
||||
onNavigate={movePage}
|
||||
|
||||
Reference in New Issue
Block a user