開源日報每天推薦一個 GitHub 優質開源項目和一篇精選英文科技或編程文章原文,堅持閱讀《開源日報》,保持每日學習的好習慣。

2024年1月11日,開源日報第1071期:
今日推薦開源項目:《serverless》
今日推薦英文原文:《Code Splitting in React: Optimize Performance by Splitting your Code》


開源項目

今日推薦開源項目:《serverless》傳送門:項目鏈接

推薦理由:⚡severless框架-使用AWS Lambda、Azure Functions、Google CloudFunctions等構建具有無伺服器架構的Web、移動和物聯網應用程序!

官網直達:serverless.com


英文原文

今日推薦英文原文:Code Splitting in React: Optimize Performance by Splitting your Code

推薦理由:Code Splitting是現代Web開發中優化React項目是比較關鍵的,使組件捆綁包拆分為較小的塊,只在特定上下文中載入所需的代碼,從而減少初始載入時間
本文主要介紹了兩種常用的實現方法:React.lazy()和Suspense,以及利用Webpack的動態導入功能


Code Splitting in React: Optimize Performance by Splitting your Code

Code splitting is an essential technique in modern web development to optimize the performance of React applications. It allows you to split your JavaScript bundle into smaller chunks. With code splitting, you can only load the code that is needed in a particular context, reducing the initial load time and improving the overall user experience. In this blog, we will explore code splitting in React and learn how to implement it in our applications.

Why is Code Splitting important?

By default, when you build a React application, all the JavaScript code is bundled into a single file. While this approach simplifies the development process, it can result in larger bundle sizes. A large bundle takes longer to load, especially on slower networks or mobile devices, leading to increased load times.

On the other hand, code splitting allows you to split your code into smaller chunks, which are loaded on-demand. This enables users to only download the necessary code when they navigate to a specific route or interact with a particular feature. As a result, the initial load time is significantly reduced, boosting the performance and user experience of the application.

Implementing Code Splitting in React

There are several ways to implement code splitting in a React application. Let』s explore two commonly used methods:

Method 1: React.lazy() with Suspense

The React.lazy() function allows you to load a component lazily, which means it is loaded only when needed. Combined with the Suspense component, you can provide a smooth loading experience while waiting for the lazily loaded component to be fetched and rendered.

Here』s an example:

import React, { lazy, Suspense } from 'react';

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

const App = () => {
  return (
    <div>
      <h1>My React App</h1>
      <Suspense fallback={<div>Loading...</div>}>
        <LazyComponent />
      </Suspense>
    </div>
  );
};

export default App;

In this example, the LazyComponent is loaded lazily using React.lazy(). The Suspense component provides a fallback UI (e.g., a loading spinner or a message) while the component is being loaded.

Method 2: Dynamic Import with Webpack

If you are using Webpack as your build tool, you can leverage its dynamic import feature to achieve code splitting.

import React, { Component } from 'react';

class App extends Component {
  handleClick = async () => {
    const module = await import('./DynamicComponent');
    // Do something with the dynamically imported module
  };

  render() {
    return (
      <div>
        <h1>My React App</h1>
        <button onClick={this.handleClick}>Load Dynamic Component</button>
      </div>
    );
  }
}

export default App;

In this example, the DynamicComponent is imported dynamically using the import() syntax. When the button is clicked, the component is loaded dynamically, giving you control over when and where to load it.

Conclusion

Code splitting is a powerful technique to optimize the performance of React applications. By splitting your code into smaller chunks and loading them on-demand, you can significantly reduce load times and provide a better user experience. React provides built-in tools like React.lazy() and Suspense, along with the support of tools like Webpack, to make code splitting easy to implement. So, start applying code splitting to your React applications and boost their performance now!

Enjoyed reading this ? Please share it with others.

Please hit 👏 button below as many times as possible to show your support!
Thanks for the read. Cheers!!!. You are Awesome !


下載開源日報APP:https://openingsource.org/2579/
加入我們:https://openingsource.org/about/join/
關注我們:https://openingsource.org/about/love/