How to Speed Up React Apps With Code Splitting
Is your React application too slow or taking too long to load? If so, you might want to use a technique known as code splitting. This technique is very effective at improving the load speed and performance of React applications. But what is code splitting? And how is it done?
What Is Code Splitting?
A typical React application comprises dozens of components (and code). But you don’t need to load most of these components when you load them for the first time. Code splitting entails splitting out the different parts of your application and only loading them when needed. This is far more efficient than loading the entire application at once.
Consider a React application that has three pages: the homepage, the about page, and the products page. When you’re on the homepage, there’s no point in loading the about page or the products page. Because you’re not actually on those pages yet. The idea of code splitting is to make sure you load the code only when it’s needed.

Open a webpage on your web browser, and then open the DevTools (it’s possible to click F12 on your keyboard to open it on Google Chrome). Next, go to the Source tab. There you’ll find all the code that’s downloaded as you navigate to the page. Without code splitting, the browser downloads all the files in your project on the initial page load. This can slow down your website if it contains a lot of files.
Code splitting becomes especially useful as your project starts to get larger and larger. This is because downloading the entire application files at once can take a very long time. So splitting that up is going to be quite beneficial.

The best part about code splitting is that you can delay the loading of components as well as functions. Ourintroductory guide on ReactJSexplains components and functions in-depth in case you need a refresher.
Code Splitting Functions: Using Dynamic Import
Consider the following situation. You want your homepage to have a button. When you click the button, you want to alert the sum of 2 and 2 (which is 4). So you create aHome.jscomponent and define the view of your homepage.
In this case, you have two options. First, you can import the code for adding the numbers at the top of theHome.jsfile. But here’s the problem. If you were to import the function at the top of the file, then the code will load even when you haven’t clicked the button. A better approach will be to load thesum()function only when you click the button.

To achieve this, you’ll need to perform a dynamic import. This means that you’ll import thesum()function inline in the button element. Here’s the code for the same:
Now the browser will only download thesum.jsmodule when you click the button. This improves the loading time of the homepage.

Code Splitting Components: Using React.lazy and Suspense
you may split components in React using thelazy()function. The best place to perform code splitting would be inside your router. Because this is where you map components to routes in your application. You can read our guide onhow to build a single-page app with React Routerif you need a refresher.
Let’s suppose that your app has aHome,About, andProductscomponent. When you’re at theHomecomponent, there’s no point in loading theAboutcomponent or theProductscomponent. So you need to split them away from theHomeroute. The following code demonstrates how to achieve that:

First, you need to import the required functions and components from thereactandreact-router-dommodules:
Next, you need to import the components dynamically using thelazy()function:
Next, set up the layout (navigation menu). Use thecomponent to render the component that corresponds to the current route (Home,About, orProductscomponent):
You can see that we wrap the components inside. This tells React that everything insidehas the potential to be lazily loaded, which means that it might not be available right away. For this reason, theSuspensecomponent has afallbackproperty. In our case, the value is simple text that says “Loading…”. So while each of the pages is being downloaded, it’s going to say loading on the screen.
Finally, set up the route:
Now when you visit the homepage, the browser loads only theHome.jsfile. In the same way, when you click on theAboutlink in the navigation menu to visit the About page, the browser loads only theAbout.jsfile. This is the same for the Products page.
Conditional Code Splitting
Oftentimes you may have content on your page that is only applicable to certain users. For example, on your homepage, you can have a section with admin data that is exclusive to admin users. This could be an admin dashboard that shows up for admin users, but not for normal users.
In this case, you wouldn’t want to show all of that data every single time. In this case, it’s possible to use the code-splitting technique to check that you only show that information if this person is an admin.
Here’s what that code would look like:
Now, when you click the toggle button,isAdminwill be set totrue.As a result, the app will show thethat is being lazily loaded. But if you’re not an admin user, then the app will never downloadAdminData.jsbecause it’s not going to need it.
Conditional code splitting uses the same concept asconditional rendering in React.
Advanced Code Splitting Concepts
One advanced technique you can enable when splitting code is transitions. TheuseTransition()hook allows you to do non-urgent updates which won’t change your UI until they’re finished updating.
First, you import the hook:
Then you call the hook, which returnsisPendingandstartTransition:
Finally, wrap the code for updating your state insidestartTransition():
Now your actual UI is not going to display the fallback value (the loading text) until the browser finishes the transition. This means that it’s going to wait for the browser to download the entire admin data before it tries to show any data at all.
Other Ways to Optimize React Performance
This article covered code splitting as a method for improving the performance of your React applications. But there are several other methods as well that can give you the required knowledge to create robust applications.
React has plenty of uses, but it can be difficult to get to grips with at first. Read all about the key practices worth following.
Your phone’s camera app doesn’t show this, so it’s easy to miss.
You can’t call this offline, Notion.
Anyone with more than a passing interest in motorsports must see these films.
Unlock a world of entertainment possibilities with this clever TV hack.
The key is not to spook your friends with over-the-top shenanigans.