Advertisement
Beginner React, Code Splitting & Lazy Loading

What is code splitting in React and why is it useful?

Short answer

Code splitting means breaking your app's JavaScript into smaller separate files instead of one huge file. The browser only downloads the small piece it needs for the current page, making the first page load much faster, especially for large apps with many different sections.

Save React mock interview
Advertisement
AModel answer
Code splitting means breaking your app's JavaScript into smaller separate files instead of one huge file. The browser only downloads the small piece it needs for the current page, making the first page load much faster, especially for large apps with many different sections.
// Instead of importing everything upfront
import Dashboard from './Dashboard';

// Split it into its own file, loaded only when needed
const Dashboard = React.lazy(() => import('./Dashboard'));
Real-world example
A large e commerce site splits its checkout page into its own chunk, so a visitor just browsing products never has to download the checkout code until they actually decide to buy something.
Likely follow-ups
  • How much smaller does an app's initial load usually get after adding code splitting?
  • What tool shows you how big each split chunk actually is?
Related topics