React.lazy से Code Splitting
In this page:
const LazyComponent = React.lazy(() => import("./LazyComponent"));
<Suspense fallback={<p>Loading...</p>}>
<LazyComponent />
</Suspense>
React.lazy से Code Split करना
React.lazy() एक function लेता है जो dynamic import() syntax call करता है, उस component के module के लिए एक Promise return करते हुए। Bundler (Vite, webpack) अपने आप उस component को अपनी separate file में split करता है, जो runtime पर सिर्फ तभी download होती है जब इसकी असल में ज़रूरत हो।
उदाहरण: Splitting Code with React.lazy
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const LazySettings = React.lazy(() =>
Promise.resolve({ default: () => <p>Settings page (loaded on demand)</p> })
);
function App() {
const [show, setShow] = React.useState(false);
return (
<div>
<button onClick={() => setShow(true)}>Load Settings</button>
{show && <React.Suspense fallback={<p>Loading settings...</p>}><LazySettings /></React.Suspense>}
</div>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
Routes को Lazy Load करना
React.lazy के सबसे common और impactful uses में से एक per-route code splitting है: आपके app का हर page अपना chunk बन जाता है, इसलिए सिर्फ homepage देखने वाला user उस admin dashboard या settings page का code कभी download नहीं करता जिसे वह कभी visit नहीं करता।
उदाहरण: Lazy Loading Routes
// Run in your local React project (npm install required)
import { lazy, Suspense } from 'react';
import { Routes, Route } from 'react-router-dom';
const Dashboard = lazy(() => import('./Dashboard'));
const Settings = lazy(() => import('./Settings'));
function AppRoutes() {
return (
<Suspense fallback={<p>Loading page...</p>}>
<Routes>
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/settings" element={<Settings />} />
</Routes>
</Suspense>
);
}
⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.
Loading Flash Tradeoff को Weigh करना
Lazy loading एक छोटे initial bundle के बदले एक brief loading state trade करता है जब पहली बार एक particular chunk की ज़रूरत हो। जिन components की हमेशा तुरंत ज़रूरत होती है (जैसे main navigation), यह tradeoff worth नहीं है — loading flash उस specific piece के लिए किसी real bundle-size benefit के बिना सिर्फ friction जोड़ता है।
उदाहरण: Weighing the Loading Flash Tradeoff
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
// Good candidate: rarely visited, large feature
const AdminPanel = React.lazy(() => Promise.resolve({ default: () => <p>Admin</p> }));
// Poor candidate: always visible immediately -- keep this in the main bundle
function MainNav() { return <nav>Always here, no lazy loading needed</nav>; }
function App() { return <MainNav />; }
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
- यह भूल जाना कि React.lazy को हमेशा इसे wrap करती एक Suspense boundary के साथ paired होना चाहिए, वरना यह एक error throw करता है।
- उन components को lazy-load करना जिनकी हमेशा तुरंत ज़रूरत होती है (जैसे main layout), बिना किसी benefit के एक unnecessary loading flash जोड़ते हुए।
- Dynamic import() function को एक default export वाले module में return होना चाहिए — React.lazy named exports को directly support नहीं करता।
- React.lazy(() => import('./Component')) एक component को इसके अपने separate JS chunk में split करता है, सिर्फ ज़रूरत पड़ने पर loaded।
- यह initial bundle size कम करता है, app की first-load performance बेहतर करते हुए।
- एक lazy component को हमेशा एक fallback वाली Suspense boundary के अंदर render होना चाहिए।
- Lazy loading के लिए अच्छे candidates: routes, modals, और rarely-used features जिनकी first render पर ज़रूरत नहीं है।
React.lazy React 16.6 से available है; bundler (Vite/webpack) के dynamic import() support करने पर निर्भर करता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Error Boundaries
- React Portals
- Modals using Portals (practical use)
- React Suspense
- Code Splitting with React.lazy
- Introduction to Server Components
- Introduction to Next.js (server-side React)
- Using React with TypeScript
- Scalable Folder Architecture
- Common React Design Patterns
- Component Documentation with Storybook
- Accessibility (a11y) in React
- i18n with react-i18next
- React Security Best Practices