Code Splitting using React lazy and React Suspense
Tags: codingtutorials, quicktipsfordevel, javascript, react
Published: 2018-12-18 05:20:49

---
title: Code Splitting using React lazy and React Suspense
published: true
tags: Coding Tutorials,Quick tips for devel,Javascript,React
canonical_url: https://codingislove.com/code-splitting-react-lazy-suspense/
---

![React lazy](https://codingislove.com/wp-content/uploads/2018/12/REACT-LAZY.png)

The initial load is critical for any application. A developer should optimize application so that application will load faster even on a slow network.  
  
Being a developer we are always interested in adding third-party libraries so that we can save time and effort of writing our own code. After this, we have to take care of the bundle size. We should make our application to load fast even after adding lot third-party libraries.

After working on react for the past two years, I could tell it was a little difficult to optimize your app using Webpack or other tools. Webpack is a great tool it provides numerous ways to optimize your app but you need to spend a <g id="7" data-gr-id="7">lot</g> of time and effort to learn and to apply them on your app.

## Code splitting

_Code splitting is a technique of dividing large bundle into small piece of  bundles and then load them on demand whenever required._

Usually, In a JS app, The entire app’s JS is bundled into one JS file and that entire JS file is loaded when the first time app is opened. This causes some wait time for the initial app to be shown which can be resolved using code splitting.

In React 16.6, React itself provides an easy way to optimize your app with minimal effort. Now I’m going to show you the optimization techniques without using any tools <g id="211" data-gr-id="211">in</g> React 16.6 with  **React.lazy and React<g id="312" data-gr-id="312">.Suspense</g>**   
  
Now we are going to learn about all these features. Let me give you <g id="324" data-gr-id="324">short</g> intro to these two features.

## React.Lazy

This function helps us to load a component on-demand just by changing a single line of code. All we need to do is replace regular import with dynamic import.

## React.Suspense

This is used to wrap the lazy loaded components with fallback (loading indicator). This fallback component is shown while a lazy component is being loaded.

## Lazy and Suspense usage

These two features will help us to optimize application by loading component when it is required and stop re-rendering when props are same as previous props.  
  
Usually, When we add external libraries to the component, all the libraries will be bundled and will be loaded into the browser on the first page load. This will work when the user has a good internet connection. What about users with a slow internet connection, slow internet user has to wait a long time to load all the code.

Lazy loading is very helpful when your application has many routes. We can load each <g id="16" data-gr-id="16">route</g> code when user <g id="17" data-gr-id="17">click</g> on <g id="12" data-gr-id="12">specific</g> route. The only difference between regular components and lazy loaded component is the way we import it.  
  
ES6 import dynamic syntax allows us to import js code dynamically. React.lazy takes <g id="14" data-gr-id="14">callback</g>. Call back should resolve to <g id="15" data-gr-id="15">module</g>. Let’s see how to import component dynamically.

This is how we import a component normally.

![](https://cdn-images-1.medium.com/max/800/0*IAiqMd7l_urjF2Jo)

This is how we import dynamic component and pass it to React.lazy.

![](https://cdn-images-1.medium.com/max/800/0*zSPfqC6-698Bp48w)

React.Suspense will wrap lazily loaded component. It will not render component until lazily loaded component requires renders. This will decrease size of bundle. Suspense will take fallback prop. fallback prop is used to show loader/spinner while lazy loaded component code is downloading. 

![](https://cdn-images-1.medium.com/max/800/0*Loo1vYLV5ZV4lS-D)

With this code, OtherComponent is lazily loaded with React.lazy. we have passed a callback function then OtherComponent is wrapped using Suspense and fallback is loading text.

Fallback accepts any react element as a value. Let me show an example of Single page application with 3 routes with lazy() and without lazy() to see how it optimizes bundle size. 

## Demo time

Let’s see a small demo. In this demo app, I have used react-router to show the demo of how individual routes are loaded on demand and dynamically.

When we load react router app in the browser all the routes will be loaded at a time and it will take time to load all the routes. With React.lazy and Suspense, we will load routes on demand.

If you see the below network requests, the first image shows without lazy and suspense that’s why the size of the bundle is 705KB, second image show with lazy and suspense code. Now the bundle size is reduced to 415 Kb initially and when you click on the second route 280 kB will be loaded on demand. This is the beauty of React.lazy and React<g id="22" data-gr-id="22">.Suspense</g>.

![](https://codingislove.com/wp-content/uploads/2018/12/Screen-Shot-2018-12-16-at-6.21.26-PM-1024x91.png)<figcaption>without React.lazy and React.Suspense</figcaption>

![](https://codingislove.com/wp-content/uploads/2018/12/Screen-Shot-2018-12-16-at-6.22.09-PM-1-1024x86.png)<figcaption>with React.lazy and React.Suspense</figcaption>

![](https://codingislove.com/wp-content/uploads/2018/12/Screen-Shot-2018-12-16-at-6.25.30-PM-1024x56.png)<figcaption>On demand second route is loaded on click on route<br></figcaption>

Source code is available [here](https://github.com/dineshu07/How-to-use-React.lazy-and-React.Suspense)

## Conclusion

React.lazy and React<g id="8" data-gr-id="8">.<g id="5" data-gr-id="5">Suspense</g></g> will help us to load our app fast. We can defer <g id="6" data-gr-id="6">rendering</g> of unwanted components and we can reduce the size of bundles ![✌](https://s.w.org/images/core/emoji/11/72x72/270c.png)

The post [Code Splitting using React lazy and React Suspense](https://codingislove.com/code-splitting-react-lazy-suspense/) appeared first on [Coding is Love](https://codingislove.com).