Skip to content

Commit 1cd7260

Browse files
committed
refactor : Code Splitting, Lazy Loading 패턴 적용
1 parent a9aaf07 commit 1cd7260

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

src/data/SelectedConstant.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export enum SelectedTitle {
2-
BAR_CHART_1 = 1,
3-
BAR_CHART_2 = 2,
4-
WATERFALL_CHART = 3,
5-
MIXED_CHART = 4,
6-
AREA_CHART = 5,
2+
BAR_CHART_1 = "BAR_CHART_1",
3+
BAR_CHART_2 = "BAR_CHART_2",
4+
WATERFALL_CHART = "WATERFALL_CHART",
5+
MIXED_CHART = "MIXED_CHART",
6+
AREA_CHART = "AREA_CHART",
77
}

src/layout/ChartLayout.tsx

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { Chart } from "chart.js";
2-
import { useEffect, useRef } from "react";
3-
import AreaChart from "../chart/AreaChart";
4-
import BarChartComparison from "../chart/BarChartComparison";
5-
import BarChartNegative from "../chart/BarChartNegative";
6-
import MixedChart from "../chart/MixedChart";
7-
import WaterfallChart from "../chart/WaterfallChart";
2+
import React, { Suspense, useEffect, useRef } from "react";
83
import { SelectedTitle } from "../data/SelectedConstant";
94

105
const ChartLayout = ({
@@ -24,20 +19,20 @@ const ChartLayout = ({
2419
};
2520
}, []);
2621

22+
const chartMap = {
23+
BAR_CHART_1: React.lazy(() => import("../chart/BarChartComparison")),
24+
BAR_CHART_2: React.lazy(() => import("../chart/BarChartNegative")),
25+
WATERFALL_CHART: React.lazy(() => import("../chart/WaterfallChart")),
26+
MIXED_CHART: React.lazy(() => import("../chart/MixedChart")),
27+
AREA_CHART: React.lazy(() => import("../chart/AreaChart")),
28+
};
29+
2730
const renderChart = () => {
28-
switch (selectedChart) {
29-
case SelectedTitle.BAR_CHART_1:
30-
return <BarChartComparison chartRef={chartRef} />;
31-
case SelectedTitle.BAR_CHART_2:
32-
return <BarChartNegative chartRef={chartRef} />;
33-
case SelectedTitle.WATERFALL_CHART:
34-
return <WaterfallChart chartRef={chartRef} />;
35-
case SelectedTitle.MIXED_CHART:
36-
return <MixedChart chartRef={chartRef} />;
37-
case SelectedTitle.AREA_CHART:
38-
return <AreaChart chartRef={chartRef} />;
39-
default:
40-
return <div>Please select a chart.</div>;
31+
if (selectedChart) {
32+
const Chart = chartMap[selectedChart];
33+
return <Chart chartRef={chartRef} />;
34+
} else {
35+
return <div>Please select a chart.</div>;
4136
}
4237
};
4338

@@ -51,7 +46,9 @@ const ChartLayout = ({
5146
boxShadow: "0px 4px 12px rgba(0, 0, 0, 0.15)",
5247
}}
5348
>
54-
<>{renderChart()}</>
49+
<Suspense fallback={<div>Loading...</div>}>
50+
<>{renderChart()}</>
51+
</Suspense>
5552
</div>
5653
);
5754
};

0 commit comments

Comments
 (0)