An explanation of code splitting in React.
Join DevzConnect — where devs connect, code, and level up together. Got questions? Stuck on a bug? Or just wanna help others crush it? Jump in and be part of a community that gets it
Welcome back to DevzConnect — where devs connect, code, and level up together. Ready to pick up where you left off? Dive back in, ask questions, share wins, or help others crush their goals!
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Code splitting is the technique of splitting your JavaScript bundle into smaller chunks that can be loaded on demand. This reduces the initial load time of your app, improving performance and user experience by only loading the necessary code for a particular page or feature.
In React, code splitting is commonly achieved through dynamic
import()statements, which allows you to load components or libraries only when they are needed.How It Works 🔧
React makes it easy to implement code splitting by using React.lazy() for dynamic imports and Suspense for handling loading states while the code is being fetched. These features enable you to split your app into multiple bundles and only load the relevant ones when the user navigates to that part of the app.
1️⃣ Using
React.lazy()for Code SplittingReact.lazy()allows you to load a component only when it’s needed (i.e., when it is rendered). This reduces the initial JavaScript load.Suspenseis used to handle the loading state while the lazy-loaded component is being fetched.Example: Code Splitting with
React.lazy()Let’s create a simple example where we split the app into multiple chunks and load them only when needed.
1. Set Up Components
Home.js– The homepage component.About.js– A second component, which will be lazily loaded.2. Using
React.lazy()andSuspense3.
About.jsComponentKey Concepts in the Example 📚
lazy():The
Aboutcomponent is wrapped withReact.lazy(), meaning it will only be loaded when it’s rendered for the first time.Suspense:Since lazy loading introduces a delay in loading the component,
Suspenseis used to display a fallback (like a loading spinner or text) while the component is being fetched.2️⃣ Dynamic Import with Routes (React Router)
Code splitting is particularly useful in single-page applications (SPAs) when you’re using routing. With React Router, you can load routes lazily.
Example with React Router
In this example:
HomeandAboutcomponents are lazy-loaded when the user navigates to those routes, reducing the initial bundle size.Benefits of Code Splitting 🏎️
3️⃣ Advanced: Splitting Libraries and Vendors
Another common strategy is splitting third-party libraries into separate bundles so they are cached separately. For example, React and React Router can be bundled separately from your app’s code.
React does this automatically in production by separating runtime code, vendor code, and your app code into separate chunks using tools like Webpack.
Summary 💡
React.lazy(): Dynamically import components to split code.Suspense: Handle the loading state while waiting for the lazy-loaded component.