Urgent.News

What's breaking now, across thousands of outlets.

Tech

Lazy Loading in React

What Is Lazy Loading? Lazy loading in React is a technique that loads a component only when it is needed instead of loading all components at the beginning of the application. It helps improve application performance by reducing the amount of JavaScript that needs to be loaded initially. For example, imagine an application with three pages: Home About Dashboard Without lazy loading, the…

Lazy loading in React is a method that loads components only when they are required, rather than loading all components simultaneously when the application starts. This technique improves the performance of the application by decreasing the initial JavaScript bundle size. For instance, consider a web application with three pages: Home, About, and Dashboard.

If lazy loading is not used, the JavaScript code for all three pages would be included in the initial bundle, even if the user never visits the About or Dashboard pages. However, when lazy loading is implemented, React loads the Home page first and subsequently loads the About or Dashboard page when the user navigates to them. The primary advantages of lazy loading include faster initial loading times, smaller bundle sizes, an enhanced user experience, loading components as needed, and better management of large applications with numerous pages and features.

To implement lazy loading in React, developers can utilize two built-in features provided by React: React.lazy() and Suspense. Here's how it works:

1. Suppose you have a component called Dashboard.jsx. First, import the necessary modules from React:

```javascript

import { lazy, Suspense } from 'react';

```

2. Define the Dashboard component using lazy loading:

```javascript

const Dashboard = lazy(() => import('./Dashboard'));

```

3. In your main component, such as App.jsx, use lazy loading and Suspense to load the Dashboard component only when needed:

```javascript

function App() {

return (

<div>

<h1>My React Application</h1>

<Suspense fallback={<p>Loading Dashboard...</p>}>

<Dashboard />

</Suspense>

</div>

);

}

```

When the user navigates to the Dashboard page, React loads the Dashboard component's code only at that moment, rather than including it in the initial bundle. This results in faster load times, a smaller bundle size, and a better overall user experience.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

A Major-style live broadcast overlay for CS2 with Game State Integration

Watching Major tournaments, the broadcast overlay always caught my eye: every player's health, money and weapons, the killfeed, the bomb timer. I wanted to build something like it for my own matches.

  • Major-style overlay displays real-time player data during CS2 matches
  • Game State Integration pulls player stats via JSON updates
  • Overlay built with HTML, CSS, JavaScript, updates at 10 updates/second

Lalibela's builders carved downward. Some of your deploys are the same

High in the Lasta mountains of northern Ethiopia, about 645 kilometres from Addis Ababa, there is a town where the churches are not built. They are carved.

  • Builders carved Lalibela churches downward from solid volcanic rock
  • Lalibela holds eleven medieval churches, UNESCO World Heritage Site since 1978
  • Construction method demands careful planning and execution, no room for mistakes

More from Monday 26 October →