EasyPerformance📖 Theory Question

What is code splitting and lazy loading in React?

💡

Hint

React.lazy + Suspense — load component code only when needed, reduces initial bundle

Full Answer

import { lazy, Suspense } from 'react';

// ❌ Eager import — bundled in the main chunk even if rarely used
import HeavyChart from './HeavyChart';

// ✅ Lazy — loaded only when rendered for the first time
const HeavyChart = lazy(() => import('./HeavyChart'));
const AdminPanel = lazy(() => import('./AdminPanel'));

// Suspense shows fallback while the chunk loads
function Dashboard() {
  return (
    }>
      
    
  );
}

// Route-based code splitting — most impactful
const Home    = lazy(() => import('./pages/Home'));
const Profile = lazy(() => import('./pages/Profile'));
const Admin   = lazy(() => import('./pages/Admin'));

function App() {
  return (
    }>
      
        } />
        } />
        } />
      
    
  );
}
💡 Route-level splitting is the highest-impact optimization. Admin panels, dashboards, and settings pages are perfect candidates — users on the landing page never need that code.

More Performance Questions

EasyWhat is React.memo and when does it actually prevent re-renders?MediumWhat is virtualization (windowing) and when do you need it?MediumWhat causes unnecessary re-renders and how do you diagnose them?MediumWhat is the React Profiler and how do you use it to diagnose performance issues?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint