⭐ 초기 로딩 시간을 단축하기 위해 사용
Dynamic imports (동적 import)
import('./MyComponent').then((module) => {
const MyComponent = module.default;
// 모듈 로드가 완료된 후에 실행될 코드
});
React.lazy
const OtherComponent = React.lazy(() => import('./OtherComponent'));
Route-based code splitting (경로 기반 코드 분할)
import React, { Suspense, lazy } from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
const Home_ = lazy(() => import('./routes/Home'));
const About_ = lazy(() => import('./routes/About'));
const App = () => (
<Router>
<Suspense fallback={<div>Loading...</div>}>
<Routes>
<Route exact path="/" element={<Home/>}/>
<Route exact path="/about" element={<About/>}/>
</Routes>
</Suspense>
</Router>
);
export default App;
<Suspense fallback={<Loading />}>
<Albums />
</Suspense>