codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

37 / 61 topics
36Optimizing React Performance37Code Splitting and Lazy Loading in React38Using Memo and ShouldComponentUpdate for Performance39Profiling React Applications
Tutorials/React.js/Code Splitting and Lazy Loading in React
⚛️React.js

Code Splitting and Lazy Loading in React

Updated 2026-04-20
3 min read

Code Splitting and Lazy Loading in React

Introduction

As web applications grow in size and complexity, they can become slow and unresponsive. One of the primary reasons for this is the large bundle sizes that are loaded when a user visits your site. Code splitting and lazy loading are optimization techniques that help reduce initial load times by breaking down your application into smaller chunks (bundles) and loading them only when needed.

In this tutorial, we will explore how to implement code splitting and lazy loading in React applications using dynamic import() syntax and the React.lazy API. By the end of this guide, you should be able to optimize your React app for better performance.

What is Code Splitting?

Code splitting is the process of breaking down your JavaScript bundle into smaller chunks that can then be loaded on demand. This reduces the initial load time by only loading the code necessary for the first render and deferring the rest until it's needed.

What is Lazy Loading?

Lazy loading, also known as on-demand loading, is a technique where you load components or resources only when they are needed. In React, this can be achieved using the React.lazy API, which allows you to dynamically import modules that will be loaded when they are rendered for the first time.

Setting Up Your React Application

Before we dive into code splitting and lazy loading, ensure your React application is set up correctly. If you don't have a React app yet, you can create one using Vite:

npm create vite@latest my-app --template react
cd my-app
npm install
npm run dev

Implementing Code Splitting

Using Dynamic import()

The dynamic import() syntax allows you to load modules asynchronously. This is the foundation of code splitting in JavaScript.

Here's an example of how to use dynamic import():

// Import a module dynamically
const MyComponent = () => import('./MyComponent');

MyComponent.then(module => {
  const Component = module.default;
  // Use the component here
});

Using React.lazy and Suspense

React.lazy is a higher-order component that lets you render a dynamic import as a regular React component. It works in conjunction with the Suspense component, which allows you to specify fallback UI while waiting for the lazy component to load.

Here's how you can use React.lazy and Suspense:

import React, { Suspense } from 'react';

const MyComponent = React.lazy(() => import('./MyComponent'));

function App() {
  return (
    <div>
      <h1>My App</h1>
      <Suspense fallback={<div>Loading...</div>}>
        <MyComponent />
      </Suspense>
    </div>
  );
}

export default App;

In this example, MyComponent is loaded lazily. The Suspense component provides a fallback UI (<div>Loading...</div>) while the component is being loaded.

Lazy Loading Routes

If you're using React Router, you can also lazy load your routes to improve performance:

import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

const Home = React.lazy(() => import('./Home'));
const About = React.lazy(() => import('./About'));

function App() {
  return (
    <Router>
      <Suspense fallback={<div>Loading...</div>}>
        <Switch>
          <Route path="/" exact component={Home} />
          <Route path="/about" component={About} />
        </Switch>
      </Suspense>
    </Router>
  );
}

export default App;

In this setup, the Home and About components are loaded lazily when their respective routes are accessed.

Best Practices

  1. Minimize Split Points: While code splitting is beneficial, it's important not to overdo it. Too many small chunks can lead to increased HTTP requests, which can negate the performance gains.
  2. Use Webpack Bundle Analyzer: Tools like webpack-bundle-analyzer can help you visualize your bundle and identify opportunities for optimization.
  3. Preload Critical Assets: Use <link rel="preload"> to preload critical assets that are needed early in the page load process.
  4. Optimize Lazy Loaded Components: Ensure that lazy-loaded components are not too large. If they are, consider further splitting them or optimizing their code.

Conclusion

Code splitting and lazy loading are powerful techniques for improving the performance of your React applications. By breaking down your application into smaller chunks and loading them only when needed, you can significantly reduce initial load times and improve user experience.

In this tutorial, we covered how to implement code splitting using dynamic import() syntax and how to use React.lazy and Suspense for lazy loading components. We also explored how to lazy load routes in React Router. By following the best practices outlined in this guide, you can optimize your React app for better performance.

Remember, performance optimization is an ongoing process. Continuously monitor your application's performance and make adjustments as needed to ensure a smooth user experience.


PreviousOptimizing React PerformanceNext Using Memo and ShouldComponentUpdate for Performance

Recommended Gear

Optimizing React PerformanceUsing Memo and ShouldComponentUpdate for Performance