React 18 Release and its Advancements

React, developed by Facebook, has been one of the most popular JavaScript libraries in the world of web development. On September 2021, React 18 alpha was released with the beta scheduled for October 2021. This new version comes with several new features and improvements which we will discuss in this article.

一、Concurrent Rendering

React 18 introduces Concurrent Rendering that allows React to pause and resume rendering as required. This means a more responsive UI and faster rendering. Let’s see how it helps in a real-life scenario:

function SearchResults({query}) {
  const [results, setResults] = useState([]);

  useEffect(() => {
    async function search() {
      const results = await fetchResults(query);
      setResults(results);
    }

    search();
  }, [query]);

  return (
    <ul>
      {results.map(result => (
        <li key={result.id}>{result.name}</li>
      ))}
    </ul>
  );
}

In the above example, we are fetching search results asynchronously from an API using useEffect. Now, if the user types in the search bar, React will terminate the previous rendering process and start rendering with the new data as soon as possible. This is made possible by Concurrent Rendering.

二、Automatic Batching

Automatic batching is another feature in React 18 that will help make our application more efficient. React will group multiple state updates into a single state update to the DOM. This way, the application doesn’t need to re-render and update the DOM multiple times for multiple state updates. This will reduce the load on the browser and lead to a faster application.

function handleClick() {
  setCount(count + 1);
  setStatus('Clicked');
}

function handleDoubleClick() {
  setCount(count + 1);
  setStatus('Double Clicked');
}

In the above example, the count is updated twice on both click and double click events. With React 18’s Automatic Batching feature, these two updates will be grouped as a single update to the DOM.

三、New APIs

React 18 also introduced some new APIs to make developers’ lives easier:

SuspenseList Component

SuspenseList component allows us to render multiple Suspense components. It can be used to show a loading indicator while waiting for data to load. Let’s see a code example:

function App() {
  return (
    <SuspenseList revealOrder="forwards">
      <Suspense fallback={<p>Loading posts...</p>}>
        <Posts/>
      </Suspense>
      <Suspense fallback={<p>Loading images...</p>}>
        <Images/>
      </Suspense>
    </SuspenseList>
  );
}

useTransition Hook

useTransition allows us to add a loading state between one action and another to prevent the user from thinking that the process is frozen. Here is how we can use it:

const [isPending, startTransition] = useTransition();

function handleButtonClick() {
  startTransition(() => {
    makeApiRequest();
  });
}

function makeApiRequest() {
  // Code to make the API request
}

四、Conclusion

React 18 brings new features that will help make our applications more responsive and efficient. With Concurrent Rendering, Automatic Batching, and new APIs, React is once again setting new standards in the world of web development. By optimizing the performance, it not only improves the user experience but also makes developers’ work much easier and productive.

原創文章,作者:VVYK,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/132565.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
VVYK的頭像VVYK
上一篇 2024-10-03 23:53
下一篇 2024-10-03 23:53

相關推薦

  • @uiw/react-amap介紹

    本文將詳細闡述@uiw/react-amap的使用方法和參數配置,以及如何在React應用中集成高德地圖組件。 一、@uiw/react-amap簡介 @uiw/react-ama…

    編程 2025-04-29
  • Webrtc音視頻開發React+Flutter+Go實戰PDF

    本文將從多個方面介紹如何使用React、Flutter和Go來進行Webrtc音視頻開發,並提供相應的代碼示例。 一、Webrtc音視頻開發介紹 Webrtc是Google開發的一…

    編程 2025-04-27
  • React簡書項目

    本文將從以下幾個方面介紹React簡書項目: 項目概述 組件分析 路由配置 Redux狀態管理 項目優化 一、項目概述 React簡書項目是一個類似於博客的Web應用,提供用戶撰寫…

    編程 2025-04-27
  • React-Icons:強大的圖標庫

    一、React-Icons的介紹 React-Icons 是一個可重用的 React 組件集合,構建了一組常見的圖標,可用於任何 React.js 項目。它為所有的圖標提供了友好的…

    編程 2025-04-25
  • React 子組件調用父組件方法

    一、基本介紹 React 是一個非常流行的 JavaScript 庫,用於構建用戶界面。React 的主要思想是組件化,允許將 UI 拆分為獨立的、可復用的部分。React 組件有…

    編程 2025-04-23
  • useMemo——提高React性能的利器

    一、什麼是useMemo React中提供了一種優化組件性能的鉤子函數——useMemo。它可以幫助我們避免在每次渲染時都執行的昂貴計算。 useMemo鉤子函數接收兩個參數:計算…

    編程 2025-04-23
  • Flutter和React Native的比較

    一、性能比較 Flutter是Google推出的移動端UI框架,最初是為了高性能而設計的。它使用Dart編寫,具有JIT和AOT兩種編譯模式,可以更好地優化性能。相比之下,Reac…

    編程 2025-04-23
  • React Context 實現原理詳解

    一、Context是什麼? Context是React提供的一種跨組件層級共享數據的方式。它解決了React組件之間數據傳遞的諸多問題。 1.1 基本用法 const ThemeC…

    編程 2025-04-23
  • React Slot詳解

    React是一個前端框架,提供了豐富的組件,隨著組件的擴展,需要對組件進行通用的封裝,其中slot是一種非常常用的方式,用於在組件中動態插入子組件或者標籤。React提供了插槽功能…

    編程 2025-04-13
  • useMemo優化React應用性能

    一、什麼是useMemo useMemo是React Hooks中的一個函數,用於優化React應用的性能。它是一個可以用來緩存函數返回結果的Hook,它根據依賴項傳入的值來決定是…

    編程 2025-04-12

發表回復

登錄後才能評論