国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 web前端 js教程 React 中用於非同步資料擷取和快取的輕量級 Hook 展示

React 中用於非同步資料擷取和快取的輕量級 Hook 展示

Jan 07, 2025 pm 06:36 PM

Showcase of a Lightweight Hook for Async Data Fetching & Caching in React

大家好!

我一直在開發(fā)一個輕量級的React hook,我稱之為useAsync,它模仿React Query 的一些基本功能(如獲取、快取、重試等),但以更緊湊的方式,易於定制的包。以下是其內(nèi)部工作原理的快速分解,引用了相關的程式碼部分。如果您想查看完整程式碼,請前往儲存庫:

GitHub 上的完整原始碼。
此鉤子也可以在 npm 上作為 api-refetch.

使用

為什麼要自己做鉤子?

雖然 React Query 和 SWR 都是很棒的函式庫,但基於以下幾個原因,我想要更實用的方法:

  1. 輕量化

    雖然 React Query 和 SWR 功能豐富,但它們可能相對較大(React Query ~2.2 MB,SWR ~620 kB)。 api-refetch 大約 250 kB,使其非常適合較小的應用程式,其中包大小是一個大問題。該掛鉤意味著作為另一個庫(Intlayer)的依賴項安裝。因此,解決方案的大小是一個重要的考慮因素。

  2. 易於自訂和最佳化
    我需要一些特定的功能,例如從本地存儲存儲/獲取資料使用簡單的方法管理並行請求。
    透過複製儲存庫將程式碼直接複製到您的專案中,您可以刪除任何不需要的功能並僅保留您需要的功能。這不僅可以減少捆綁包大小,還可以最大限度地減少不必要的重新渲染和增加,為您提供根據(jù)您的特定要求量身定制的更精簡、性能更高的解決方案。

  3. 無所需供應商

    我想避免使用 Context Provider 來使鉤子全局化,並使其使用盡可能簡單。所以我根據(jù) Zustand 商店製作了 hooke 的一個版本(請參閱下面的範例)。

  4. 學習練習

    從頭開始建立非同步函式庫是理解並發(fā)、快取和狀態(tài)管理內(nèi)部結(jié)構(gòu)的絕佳方法。

簡而言之,滾動我自己的鉤子是一個機會精確磨練我需要的功能(並跳過我不需要的功能),同時保持庫小且易於理解。

涵蓋的功能

React 鉤子管理:

  • 取得與狀態(tài)管理:處理載入、錯誤、成功和取得狀態(tài)。
  • 快取和儲存:可選擇快取資料(透過底層的 React 狀態(tài)或 Zustand)並提供本地儲存支援。
  • 重試和重新驗證:可設定的重試限制和自動重新驗證間隔。
  • 啟動和失效:根據(jù)其他查詢或狀態(tài)自動啟動和失效查詢。範例:使用者登入時自動取得一些數(shù)據(jù),並在使用者登出時使其失效。
  • 並行元件掛載取得:當多個元件同時掛載時,防止對相同資源同時發(fā)出多個要求。

程式碼如何運作

以下是 api-refetch 中的要點以及對 useAsync.tsx 中程式碼相關部分的簡短引用。

1. 並行安裝的取得與處理

  • 程式碼片段
  // This map stores any in-progress Promise to avoid sending parallel requests
  // for the same resource across multiple components.
  const pendingPromises = new Map();

  const fetch: T = async (...args) => {
    // Check if a request with the same key + args is already running
    if (pendingPromises.has(keyWithArgs)) {
      return pendingPromises.get(keyWithArgs);
    }

    // Otherwise, store a new Promise and execute
    const promise = (async () => {
      setQueryState(keyWithArgs, { isLoading: true });

      // ...perform fetch here...
    })();

    // Keep it in the map until it resolves or rejects
    pendingPromises.set(keyWithArgs, promise);
    return await promise;
  };
  • 說明:在這裡,我們將任何正在進行的提取儲存在pendingPromises 映射中。當兩個元件嘗試同時取得相同的資源(透過具有相同的 keyWithArgs)時,第二個元件只會重複使用正在進行的請求,而不是重複的網(wǎng)路呼叫。

2. 重新驗證

  • 程式碼片段
  // Handle periodic revalidation if caching is enabled
  useEffect(
    () => {
      if (!revalidationEnabled || revalidateTime <= 0) return; // Revalidation is disabled
      if (!isEnabled || !enabled) return; // Hook is disabled
      if (isLoading) return; // Fetch is already in progress
      if (!isSuccess || !fetchedDateTime) return; // Should retry either of revalidate
      if (!(cacheEnabled || storeEnabled)) return; // Useless to revalidate if caching is disabled

      const timeout = setTimeout(() => {
        fetch(...storedArgsRef.current);
      }, revalidateTime);

      return () => clearTimeout(timeout);
    },
    [
      /* dependencies */
    ]
  );
  • 說明:每當您啟用重新驗證時,api-refetch 都會檢查快取的資料是否早於指定的 revalidateTime。如果是,資料會在背景自動重新獲取,以使您的 UI 保持同步,無需額外的手動觸發(fā)。

3. 重試邏輯

  • 程式碼片段
  useEffect(
    () => {
      const isRetryEnabled = errorCount > 0 && retryLimit > 0;
      const isRetryLimitReached = errorCount > retryLimit;

      if (!isEnabled || !enabled) return; // Hook is disabled
      if (!isRetryEnabled) return; // Retry is disabled
      if (isRetryLimitReached) return; // Retry limit has been reached
      if (!(cacheEnabled || storeEnabled)) return; // Useless to retry if caching is disabled
      if (isLoading) return; // Fetch is already in progress
      if (isSuccess) return; // Hook has already fetched successfully

      const timeout = setTimeout(() => {
        fetch(...storedArgsRef.current);
      }, retryTime);

      return () => clearTimeout(timeout);
    },
    [
      /* dependencies */
    ]
  );
  • 解釋:發(fā)生錯誤時,掛鉤會計算已發(fā)生的失敗嘗試次數(shù)。如果它仍然低於 retryLimit,它會自動等待 retryTime 毫秒,然後再試一次。此過程將持續(xù)進行,直到成功獲取數(shù)據(jù)或達到重試限制。

4. 自動獲取

  • 程式碼片段
  // Auto-fetch data on hook mount if autoFetch is true
  useEffect(
    () => {
      if (!autoFetch) return; // Auto-fetch is disabled
      if (!isEnabled || !enabled) return; // Hook is disabled
      if (isFetched && !isInvalidated) return; // Hook have already fetched or invalidated
      if (isLoading) return; // Fetch is already in progress

      fetch(...storedArgsRef.current);
    },
    [
      /* dependencies */
    ]
  );
  • 說明:將 autoFetch 設為 true 時,鉤子會在元件安裝後立即自動執(zhí)行非同步函數(shù),非常適合您始終希望載入資料的「即發(fā)即忘」場景。

查看 GitHub 上的完整原始碼

查看完整程式碼,其中包含本機儲存邏輯、查詢失效等:

  • 完整原始碼

如果您有興趣,請隨意嘗試、回報問題或做出貢獻。非常感謝任何反饋!

使用範例

安裝

複製程式碼或編碼(repo)[https://github.com/aymericzip/api-refetch]


  // This map stores any in-progress Promise to avoid sending parallel requests
  // for the same resource across multiple components.
  const pendingPromises = new Map();

  const fetch: T = async (...args) => {
    // Check if a request with the same key + args is already running
    if (pendingPromises.has(keyWithArgs)) {
      return pendingPromises.get(keyWithArgs);
    }

    // Otherwise, store a new Promise and execute
    const promise = (async () => {
      setQueryState(keyWithArgs, { isLoading: true });

      // ...perform fetch here...
    })();

    // Keep it in the map until it resolves or rejects
    pendingPromises.set(keyWithArgs, promise);
    return await promise;
  };

快速範例

  // Handle periodic revalidation if caching is enabled
  useEffect(
    () => {
      if (!revalidationEnabled || revalidateTime <= 0) return; // Revalidation is disabled
      if (!isEnabled || !enabled) return; // Hook is disabled
      if (isLoading) return; // Fetch is already in progress
      if (!isSuccess || !fetchedDateTime) return; // Should retry either of revalidate
      if (!(cacheEnabled || storeEnabled)) return; // Useless to revalidate if caching is disabled

      const timeout = setTimeout(() => {
        fetch(...storedArgsRef.current);
      }, revalidateTime);

      return () => clearTimeout(timeout);
    },
    [
      /* dependencies */
    ]
  );

就是這樣!嘗試一下,然後讓我知道進展如何。 GitHub 上非常歡迎提供回饋、問題或貢獻。

GitHub: api-refetch

編碼愉快!

以上是React 中用於非同步資料擷取和快取的輕量級 Hook 展示的詳細內(nèi)容。更多資訊請關注PHP中文網(wǎng)其他相關文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內(nèi)容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Java vs. JavaScript:清除混亂 Java vs. JavaScript:清除混亂 Jun 20, 2025 am 12:27 AM

Java和JavaScript是不同的編程語言,各自適用於不同的應用場景。 Java用於大型企業(yè)和移動應用開發(fā),而JavaScript主要用於網(wǎng)頁開發(fā)。

JavaScript評論:簡短說明 JavaScript評論:簡短說明 Jun 19, 2025 am 12:40 AM

JavascriptconcommentsenceenceEncorenceEnterential gransimenting,reading and guidingCodeeXecution.1)單inecommentsareusedforquickexplanations.2)多l(xiāng)inecommentsexplaincomplexlogicorprovideDocumentation.3)

如何在JS中與日期和時間合作? 如何在JS中與日期和時間合作? Jul 01, 2025 am 01:27 AM

JavaScript中的日期和時間處理需注意以下幾點:1.創(chuàng)建Date對像有多種方式,推薦使用ISO格式字符串以保證兼容性;2.獲取和設置時間信息可用get和set方法,注意月份從0開始;3.手動格式化日期需拼接字符串,也可使用第三方庫;4.處理時區(qū)問題建議使用支持時區(qū)的庫,如Luxon。掌握這些要點能有效避免常見錯誤。

為什麼要將標籤放在的底部? 為什麼要將標籤放在的底部? Jul 02, 2025 am 01:22 AM

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScript與Java:開發(fā)人員的全面比較 JavaScript與Java:開發(fā)人員的全面比較 Jun 20, 2025 am 12:21 AM

JavaScriptIspreferredforredforwebdevelverment,而Javaisbetterforlarge-ScalebackendsystystemsandSandAndRoidApps.1)JavascriptexcelcelsincreatingInteractiveWebexperienceswebexperienceswithitswithitsdynamicnnamicnnamicnnamicnnamicnemicnemicnemicnemicnemicnemicnemicnemicnddommanipulation.2)

什麼是在DOM中冒泡和捕獲的事件? 什麼是在DOM中冒泡和捕獲的事件? Jul 02, 2025 am 01:19 AM

事件捕獲和冒泡是DOM中事件傳播的兩個階段,捕獲是從頂層向下到目標元素,冒泡是從目標元素向上傳播到頂層。 1.事件捕獲通過addEventListener的useCapture參數(shù)設為true實現(xiàn);2.事件冒泡是默認行為,useCapture設為false或省略;3.可使用event.stopPropagation()阻止事件傳播;4.冒泡支持事件委託,提高動態(tài)內(nèi)容處理效率;5.捕獲可用於提前攔截事件,如日誌記錄或錯誤處理。了解這兩個階段有助於精確控制JavaScript響應用戶操作的時機和方式。

JavaScript:探索用於高效編碼的數(shù)據(jù)類型 JavaScript:探索用於高效編碼的數(shù)據(jù)類型 Jun 20, 2025 am 12:46 AM

javascripthassevenfundaMentalDatatypes:數(shù)字,弦,布爾值,未定義,null,object和symbol.1)numberSeadUble-eaduble-ecisionFormat,forwidevaluerangesbutbecautious.2)

Java和JavaScript有什麼區(qū)別? Java和JavaScript有什麼區(qū)別? Jun 17, 2025 am 09:17 AM

Java和JavaScript是不同的編程語言。 1.Java是靜態(tài)類型、編譯型語言,適用於企業(yè)應用和大型系統(tǒng)。 2.JavaScript是動態(tài)類型、解釋型語言,主要用於網(wǎng)頁交互和前端開發(fā)。

See all articles