How to use lazy loading in react to reduce the first screen loading time

How to use lazy loading in react to reduce the first screen loading time

Recently I'm writing a react-ant-admin integration framework for quickly launching mid- and back-end projects. There are many problems encountered, the most important of which should be the access speed. I just wonder if React can use lazy loading of routes like Vue to reduce the time spent on home page rendering.

So I found a very useful wheel: @loadable/component

use

Install

npm install @loadable/component -D
# or use yarn
yarn add @loadable/component -D

How to use it in routing?

In the src/router/index.js file, write as follows:

import React from "react";
import { Route, Switch } from "react-router-dom";
import routerList from "./routes";

const router = () => {
  return (
    <Switch>
      {routerList.map((item) => {
        const { component: Component, key, path, ...itemProps } = item;
        return (
          <Route
            exact={true}
            key={key}
            path={path}
            render={(allProps) => <Component {...allProps} {...itemProps} />}
          />
        );
      })}
    </Switch>
  );
};

export default router;

In src/router/routes.js file, write as follows:

import loadable from "@loadable/component";

const Error404 = loadable(() => import("@/pages/err/404")); // Corresponding file src/pages/err/404.js
const Home = loadable(() => import("@/pages/home"));
const Demo = loadable(() => import("@/pages/demo"));

const routerList = [
  {
    path: "/",
    key: "home",
    components: Home,
  },
  {
    path: "/demo",
    key: "demo",
    components: Demo,
  },
  {
    path: "*",
    key: "404",
    components: Error404,
  },
];

export default routerList;

In the src/App.js file, write as follows:

import React from "react";
import { BrowserRouter as Router } from "react-router-dom";
import Routes from "./router";
export default function App() {
  return (
    <Router>
      <Routes />
    </Router>
  );
}

At this point, you can go to the page to check whether the js file is dynamically loaded when switching routes. If the js file is loaded after switching the route, it means the lazy loading route is successful!

Loading speed comparison

Before using @loadable/component the server bandwidth was 1M, gzip compression, the file size was about 2MB, and the server request loading time was about 4.3s

Use routing lazy loading. The server bandwidth is 1M, gzip compression, the file size is about 1MB, and the server request loading time is about 1s.

The above is the details of how React uses lazy loading to reduce the first screen loading time. For more information about how React lazy loading can reduce loading time, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • React implementation example using Amap (react-amap)
  • Detailed explanation of virtual DOM and diff algorithm in react
  • React example showing file upload progress
  • How to run the react project on WeChat official account
  • How to write CSS elegantly with react
  • Encapsulate a simplest ErrorBoundary component to handle react exceptions
  • React Fiber structure creation steps
  • Detailed explanation of the use of Refs in React's three major attributes
  • Write a React-like framework from scratch
  • Understanding and using React useEffect

<<:  A brief analysis of the game kimono memo problem

>>:  Detailed explanation of commonly used nginx rewrite rules

Recommend

Introducing ECharts into the Vue project

Table of contents 1. Installation 2. Introduction...

Vue realizes price calendar effect

This article example shares the specific code of ...

Script example for starting and stopping spring boot projects in Linux

There are three ways to start a springboot projec...

Solution to mysql error when modifying sql_mode

Table of contents A murder caused by ERR 1067 The...

JavaScript implements page scrolling animation

Table of contents Create a layout Add CSS styles ...

In-depth explanation of MySQL learning engine, explain and permissions

engine Introduction Innodb engine The Innodb engi...

Implementing a table scrolling carousel effect through CSS animation

An application of CSS animation, with the same co...

Will this SQL writing method really cause the index to fail?

Preface There are often some articles on the Inte...

Vue and react in detail

Table of contents 1. Panorama II. Background 1. R...

MySQL replication advantages and principles explained in detail

Replication is to transfer the DDL and DML operat...

How to change the root password of Mysql5.7.10 on MAC

First, start MySQL in skip-grant-tables mode: mys...

Is your website suitable for IE8?

During the Olympic Games, IE 8 Beta 2 will be rele...

Detailed explanation of Vue3.0 + TypeScript + Vite first experience

Table of contents Project Creation Project Struct...