Code splitting is a performance optimization technique in React that breaks the JavaScript bundle into smaller chunks to reduce initial load time and improve application performance.
import()
.React.lazy
to split code into separate chunks, like const LazyComponent = lazy(() => import('./MyComponent'));
Suspense
, like {`Loading...
import React, { Suspense, lazy } from 'react';
const LazyComponent = lazy(() => import('./MyComponent'));
const App = () => {
return (
<div>
<h1>Main App</h1>
{/* Show fallback while LazyComponent is loading */}
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
</div>
);
};
Suspense
.Code splitting in React improves performance by reducing JavaScript sent upfront. By leveraging React.lazy
and Suspense
, developers can create faster, more scalable apps that load only what is needed.