Skip to main content

Command Palette

Search for a command to run...

React performance

Published
•1 min read•View as Markdown
React performance

Lazy Loading Components

Lets say we have a simple application in which we are returning just two components and the second component is only visible when a button is clicked. The problem here is that the second component needs to be not loaded by the browser every time because when the component is not mounted the runtime still renders its complete javascript code and with these some problems are associated: 1)It consumes the bandwidth 2) makes the process of loading all the components slow because the browser needs to render more components. Hence it becomes a problem in case of large scale applications

The solution for this is code splitting.

How this works is that lets see with the help of an example:

image.png

image.png

Notice that when the component is mounted or not the same amount of network requests are made in both the cases which is not feasible. When the component is not mounted less network requests should be made

image.png

However we get an error with this:

image.png

This is because when the first time the component function is called the component is not available inside the App.js file so we need some fallback UI which shows that the component is still loading

This UI can be achieved by using the React.Suspense element

image.png

More from this blog

Dhruv's Web development journey

64 posts